-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
31 lines (26 loc) · 884 Bytes
/
solution.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
from typing import List
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.binarySearch(nums, target, True)
right = self.binarySearch(nums, target, False)
return [left, right]
def binarySearch(self, nums, target, leftBias):
left, right = 0, len(nums)-1
i = -1
while left <= right:
middle = (left + right) // 2
if target > nums[middle]:
left = middle + 1
elif target < nums[middle]:
right = middle - 1
else:
i = middle
if leftBias:
right = middle - 1
else:
left = middle + 1
return i
if __name__ == '__main__':
nums = [5, 7, 7, 8, 8, 10]
target = 8
print(Solution().searchRange(nums, target))