class Solution: # @param A, a list of integers # @param target, an integer to be searched # @return a list of length 2, [index1, index2] def searchRange(self, A, target): left,right,idx=0,len(A)-1,-1 while left<=right: mid=(left+right)//2 if A[mid]==target: idx=mid break elif A[mid]<target: left=mid+1 elif A[mid]>target: right=mid-1 if idx==-1: return [-1,-1] rl,rr=left,right ml=mr=mid while rl<ml: mml=(rl+ml)//2 if A[mml]==target: ml=mml else: rl=mml+1 while rr>mr: mmr=-((-rr-mr)//2) if A[mmr]==target: mr=mmr else: rr=mmr-1 return [rl, rr]
Friday, September 12, 2014
Leetcode: Search for a Range @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment