forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode34.py
47 lines (36 loc) · 1.16 KB
/
leetcode34.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
# Time Complexity : O(lon n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Your code here along with comments explaining your approach
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def start():
l=0
u=len(nums)-1
while l<=u:
mid=(l+u)//2
if nums[mid]<target:
l=mid+1
else:
u=mid-1
if l<len(nums) and nums[l]==target:
return l
else:
return -1
def end():
l=0
u=len(nums)-1
while l<=u:
mid=(l+u)//2
if nums[mid]>target:
u=mid-1
else:
l=mid+1
if u<len(nums) and nums[u]==target:
return u
else:
return -1
if not nums:
return [-1,-1]
return (start(),end())