forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_for_a_range.py
More file actions
36 lines (34 loc) · 1.13 KB
/
search_for_a_range.py
File metadata and controls
36 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
class Solution:
"""
@param A : a list of integers
@param target : an integer to be searched
@return : a list of length 2, [index1, index2]
"""
def searchRange(self, A, target):
# write your code here
i = self.bSearch(A, 0, len(A), target)
if i == -1:
return [-1, -1]
start, end = i, i
while i != -1: # 前半区不停二分,直到找不到target为止
start = i
i = self.bSearch(A, 0, start, target)
if end < len(A) - 1:
i = end
while (i != -1) and (i <= (len(A) - 1)):
end = i
i = self.bSearch(A, end + 1, len(A), target)
if i == -1:
break
return [start, end]
def bSearch(self, array, start, end, target):
while start < end:
mid = (start + end) / 2
if array[mid] == target:
return mid
elif array[mid] < target:
start = mid + 1
else:
end = mid # end是开区间,所以不用减1。
return -1