48ms python solution

  • 0
    class Solution:
        # @param {integer[][]} matrix
        # @param {integer} target
        # @return {boolean}
        def searchMatrix(self, matrix, target):
            if target < matrix[0][0] or target > matrix[-1][-1] or not matrix:
                return False
            m = len(matrix)
            n = len(matrix[0])
            for i in range(m):
                if matrix[i][0] > target:
                    m = i 
            for i in range(n):
                #Do not forget to check the last row
                if matrix[m-1][i] == target or matrix[-1][i] == target:
                    return True
            return False

Log in to reply

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