Skip to content

Commit 36fcc85

Browse files
Question 34
1 parent 8ce34c2 commit 36fcc85

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

34. Find First and Last Position of Element in Sorted Array.py

+27
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@
1313
Output: [-1,-1]
1414
'''
1515

16+
class Solution:
17+
def searchRange(self, nums, target):
18+
if len(nums)==0:
19+
return [-1, -1]
20+
result = [-1, -1]
21+
l = len(nums)
22+
i = 0
23+
j = l-1
24+
while(i<j):
25+
mid = i + (j-i)//2
26+
if nums[mid]<target:
27+
i = mid+1
28+
else:
29+
j = mid
30+
if nums[i] != target:
31+
return result
32+
result[0]=i
33+
j = l-1
34+
while(i<j):
35+
print(i,j)
36+
mid = i + (j-i)//2 + 1
37+
if nums[mid]>target:
38+
j = mid-1
39+
else:
40+
i = mid
41+
result[1]=i
42+
return result

0 commit comments

Comments
 (0)