Skip to content

Commit 5b1c6f0

Browse files
committed
Add python solution (34)
1 parent 281eaa1 commit 5b1c6f0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def searchRange(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
left = 0
9+
right = len(nums)-1
10+
def binarySearch(nums,target,left,right):
11+
while (left <= right):
12+
mid = left + (right-left) / 2
13+
if(target > nums[mid]):
14+
left = mid + 1
15+
elif(target < nums[mid]):
16+
right = mid -1
17+
else:
18+
return mid
19+
return -1
20+
index = binarySearch(nums,target,left,right)
21+
if index == -1: return [-1,-1]
22+
index2 = index
23+
while index -1 >=0 and nums[index - 1] == target: index -=1
24+
while index2+1 < len(nums) and nums[index2 + 1] == target: index2 +=1
25+
return[index,index2]
26+
27+

0 commit comments

Comments
 (0)