Simple Python Solution


  • 0
    G
    def searchRange(self, A, B):
        return self.helper(A,B,0,len(A)-1)
        
    def helper(self,A,B,start,end):
        if B<A[start] or B>A[end]:
            return [-1,-1]
        if A[start]==B and A[end]==B:
            return [start,end]
        else:
            mid=(start+end)/2
            if A[mid]==B:
                s=mid-1
                e=mid+1
                while s>=start or e<=end:
                    if s!=start-1:
                        if A[s]==B:
                            s-=1
                    if e!=end+1:
                        if A[e]==B:
                            e+=1
                    if (s<start or e>end) or (A[s]!=B and A[e]!=B):
                        break
                    
                return [s+1,e-1]
            elif A[mid]<B:
                return self.helper(A,B,mid+1,end)
            else:
                return self.helper(A,B,start,mid-1)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.