Simple python solution

  • 0

    Start from top right corner of the matrix. Move left if the target is smaller and move down if the target is greater.

    class Solution(object):
        def searchMatrix(self, matrix, target):
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            if not matrix:
                return False
            m = len(matrix)
            n = len(matrix[0])
            j = n - 1
            i = 0
            while j >= 0 and i < m:
                if matrix[i][j] == target:
                    return True
                if matrix[i][j] > target:
                    j -= 1
                    i += 1
            return False

Log in to reply

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