题目
思路
二分查找
代码
class Solution:
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
left = 0; right = len(nums) - 1
leftIndex = -1; rightIndex = -1
while left <= right:
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
leftIndex = mid
right = mid - 1
left = 0; right = len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
rightIndex = mid
left = mid + 1
return [leftIndex, rightIndex]