Python solution


  • 0
    P
    class Solution(object):
        def searchMatrix(self, matrix, target):
            if len(matrix)==0:
                return False
            if len(matrix[0])==0:
                return False
            
            rows=len(matrix)
            columns=len(matrix[0])
            first=[]
            lst=[]
            for i in range(0,rows):
                lst=(matrix[i])
                first.append(lst[0])
            if target<first[0]:
                return False
            val=self.binarySearch(first,target)
            if val==True:
                return True
            else:
                count=0
                for i in range(0,len(first)):
                    if first[i]<target:
                        count+=1
                count-=1
                print("count {}".format(count))
                for i in range(count,-1,-1):
                    val=self.binarySearch(matrix[i],target)
                    if val==True:
                        return True
                
                return False
        
        def binarySearch(self,lst,target):
            start=0
            end=len(lst)-1
            found=False
            print("target {}".format(target))
            while (start<=end) and found==False:
                mid=int(start+end)/2
                print(lst[mid])
                if lst[mid]>target:
                    end=mid-1
                elif lst[mid]<target:
                    start=mid+1
                elif lst[mid]==target:
                    print("Hello")
                    return True
            return found
    

Log in to reply
 

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