3 solution python


  • 0
    Y

    '''
    def searchMatrix(self, matrix, target):

        # solution3
        
        l, r = 0, len(matrix)-1
        mid=(l+r)//2
        while l<r:
            mid=(l+r)//2
            if matrix[mid][-1]==target or matrix[mid][0]==target:
                return True
            if matrix[mid][-1]<target:
                l=mid+1
                continue
            if matrix[mid][0]>target:
                r=mid-1
                continue
            r=l=mid
        if l==r+1:
            return False
        ls, rs = 0, len(matrix[0])-1
        matrix=matrix[r]
        while ls<=rs:
            mids=(ls+rs)//2
            if matrix[mids]==target:
                return True
            elif matrix[mids]<target:
                ls=mids+1
            else:
                rs=mids-1
        return False
        
        
        #solution 2
        return any(target in row for row in matrix if row[0]<= target)
        
        #solution 1
        self.a=False
        def helper(l,r):
            if l>r:
                return False
            mid=(l+r)//2
            mid_val=matrix[mid//len(matrix[0])][mid%len(matrix[0])]
            if mid_val>target:
                helper(l,mid-1)
                print l,r
            elif mid_val<target:
                helper(mid+1,r)
            else:
                self.a=True
                return True
                
        if target<matrix[0][0] or target>matrix[-1][-1]:
            return False
        elif target==matrix[0][0] or target==matrix[-1][-1]:
            return True
        else:
            helper(0,len(matrix[0])*len(matrix)-1)
            return self.a
    

    '''


Log in to reply
 

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