class Solution(object):
def searchRange(self, nums, target):"""
:type nums: List[int]:type target:int:rtype: List[int]"""
# 二分查找法
n =len(nums)if not n:return[-1,-1]
l, r =0, n-1while l<=r:
mid = l +((r-l)>>1)if nums[mid]>target:
r = mid-1
elif nums[mid]< target:
l = mid +1else:
# 找到target后,还需要找到第一个target跟最后一个target
# 先让l,r都等于mid
#l往前找第一个等于target的,r找最后一个等于target的
l, r = mid, mid
while l-1>=0 and nums[l-1]== target:
l = l-1while r+1<=n-1 and nums[r+1]== target:
r = r+1return[l,r]return[-1,-1]