python二分法模板
def searchLeft(i,j,nums,target):
while i<=j:
mid = (i+j)>>1
if target<=nums[mid]:
j = mid-1
else:
i = mid+1
if i>=len(nums) or nums[i] != target:
return -1
return i
def searchRight(i,j,nums,target):
while i<=j:
mid = (i+j)>>1
if target < nums[mid]:
j = mid-1
else:
i = mid+1
if j<0 or nums[j] != target:
return -1
return j
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
i = 0
j = len(nums)-1
return [searchLeft(i,j,nums,target),searchRight(i,j,nums,target)]