forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
41 lines (33 loc) · 968 Bytes
/
Solution.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
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if not nums or target < nums[0] or target > nums[-1]:
return [-1, -1]
# Finding the lower limit
left = 0
right = len(nums)
while left < right:
middle = (left + right)/2
if nums[middle] >= target:
right = middle
else:
left = middle + 1
low = left
# Finding the higher limit
left = 0
right = len(nums)
while left < right-1:
middle = (left + right) / 2
if nums[middle] > target:
right = middle
else:
left = middle
high = left
if nums[low] == target and nums[high] == target:
return [low, high]
else:
return [-1, -1]