def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
l,r=0,len(nums)-1
start=-1
end=-1
for i in range(len(nums)):
if nums[l]==target:
start=l
if nums[r]==target:
end=r
if nums[l]<target:
l+=1
elif nums[r]>target:
r-=1
return [start,end]
O(N)
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
ll=0
rr=1
start=self.subtree(nums,target,ll)
end=self.subtree(nums,target,rr)
return [start,end]
def subtree(self,nums,target,loor):
l,r=0,len(nums)-1
start=-1
end=-1
if loor==0:
while l<=r:
mid=l+(r-l)/2
if nums[mid]==target:
start=mid
r=mid-1
elif nums[mid]<target:
l=mid+1
else:
r=mid-1
return start
if loor==1:
while l<=r:
mid=l+(r-l)/2
if nums[mid]==target:
end=mid
l=mid+1
elif nums[mid]<target:
l=mid+1
else:
r=mid-1
return end
O(logn)