-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path034__Search For A Range.py
More file actions
41 lines (40 loc) · 1.18 KB
/
Copy path034__Search For A Range.py
File metadata and controls
41 lines (40 loc) · 1.18 KB
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:
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if not nums:
return([-1,-1])
if len(nums)==1:
if nums[0]==target:
return([0,0])
else:
return([-1,-1])
a=0
b=len(nums)-1
def search(a,b,nums,target,list,result):
e=int((a+b)/2)
if a<=b:
if nums[e]==target:
c=search(a,e-1,nums,target,list,result)
d=search(e+1,b,nums,target,list,result)
return c+[e]+d
elif nums[e]>target:
return search(a,e-1,nums,target,list,result)
elif nums[e]<target:
return search(e+1,b,nums,target,list,result)
elif a > b:
result=list
return(result)
list=[]
result=[]
result=search(a,b,nums,target,list,result)
if result==[]:
return([-1,-1])
else:
return([result[0],result[-1]])
a=Solution()
b=a.searchRange([5, 7, 7, 7, 9, 10],8)
print(b)