Python Solution(beat over 96.3%) time: O(m + n) space: O(1)


  • 1
    J
    def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
     
            try:
                N, M = len(matrix), len(matrix[0])
                i, j = 0, M - 1
                while(1):
                    cur = matrix[i][j]
                    if(target == cur):
                        return True
                    elif(target > cur):
                        i += 1
                    else:
                        j -= 1
            except:
                return False
    

    without any redundant border check. use try, except to catch the last "search failure" and the special empty case.


Log in to reply
 

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