day21笔记
1.题目描述
2.代码构思(debug)
二分法
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
l = 0
r = len(nums) -1
while l <= r:
mid = (l + r) // 2
if nums[mid] == target:
tmp = mid
while tmp > 0 and nums[tmp-1] == target:
tmp -= 1
while mid < len(nums) -1 and nums[mid+1] == target:
mid += 1
return [tmp,mid]
elif nums[mid] > target:
r = mid - 1
else:
l = mid + 1
return [-1,-1]