-
Notifications
You must be signed in to change notification settings - Fork 0
/
34.py
33 lines (30 loc) · 950 Bytes
/
34.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
class Solution(object):
def searchRange(self, nums, target):
if not nums:
return [-1, -1]
def leftIndex(nums, target):
l, r = 0, len(nums)-1
while l<=r:
mid = (l+r)//2
if nums[mid]<target:
l = mid+1
else:
r = mid-1
return l
def rightIndex(nums, target):
l, r = 0, len(nums)-1
while l<=r:
mid = (l+r)//2
if nums[mid]<=target:
l = mid+1
else:
r = mid-1
return r
left, right = leftIndex(nums, target), rightIndex(nums, target)
if left <= right and left<len(nums) and nums[left] == target and nums[right] == target:
return left, right
else:
return [-1, -1]
# Runtime - 63ms
# Memory - 12.70MB
# Difficulty - Medium