- class Solution:
- def searchRange(self, nums: List[int], target: int) -> List[int]:
- l=0
- r=len(nums)-1
- def serach_left(l,r):
- while l <= r:
- mid=l+(r-l)//2
- if nums[mid]==target:
- if(mid==0 or nums[mid-1]!=target):
- return mid
- else:
- r=mid-1
- elif nums[mid]>target:
- r=mid-1
- else:
- l=mid+1
- return -1
- def serach_right(l,r):
- while l <= r:
- mid=l+(r-l)//2
- if nums[mid]==target:
- if(mid==len(nums)-1 or nums[mid+1]!=target):
- return mid
- else:
- l=mid+1
- elif nums[mid]>target:
- r=mid-1
- else:
- l=mid+1
- return -1
- res=[]
- res.append(serach_left(l,r))
- res.append(serach_right(l,r))
- return res
改了正常二分里的if nums[mid]==target:部分