-
Notifications
You must be signed in to change notification settings - Fork 0
/
034.py
38 lines (33 loc) · 975 Bytes
/
034.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
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if not nums:
return [-1, -1]
up_i, up_j = 0, len(nums)-1
while up_i < up_j:
mid = (up_i+up_j) / 2
if mid == up_i:
if nums[up_j] != target:
up_j = up_i
else:
up_i = up_j
break
if nums[mid] <= target:
up_i = mid
else:
up_j = mid - 1
lo_i, lo_j = 0, len(nums)-1
while lo_i < lo_j:
mid = (lo_i+lo_j) / 2
if nums[mid] >= target:
lo_j = mid
else:
lo_i = mid + 1
if nums[up_j] == target:
return [lo_i, up_i]
else:
return [-1, -1]