classSolution: defsearchRange(self, nums: List[int], target: int) -> List[int]: res = [-1, -1] ifnot nums: return res iflen(nums) == 1: return [0, 0] if nums[0] == target else res
left = 0 right = len(nums) - 1 # 寻找左边界 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: right = mid - 1 elif nums[mid] < target: left = mid + 1 elif nums[mid] > target: right = mid - 1 if left < len(nums) and nums[left] == target: res[0] = left
# 寻找右边界 left = 0 right = len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: left = mid + 1 elif nums[mid] < target: left = mid + 1 elif nums[mid] > target: right = mid - 1 if right > -1and nums[right] == target: res[1] = right