forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch_for_a_Range.py
56 lines (47 loc) · 1.4 KB
/
Search_for_a_Range.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
"""
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):
start = 0
end = len(A) - 1
bound = [-1, -1]
# Check for left bound
while start + 1 < end:
mid = (start + end) / 2
if A[mid] == target:
end = mid
elif A[mid] < target:
start = mid
else:
end = mid
if A[start] == target:
bound[0] = start
elif A[end] == target:
bound[0] = end
else:
return bound
# Check right bound
start = 0
end = len(A) - 1
while start + 1 < end:
mid = (start + end) / 2
if A[mid] == target:
start = mid
elif A[mid] < target:
start = mid
else:
end = mid
if A[end] == target:
bound[1] = end
elif A[start] == target:
bound[1] = start
return bound