Easy to understand Python solution beats 83% of submissions


  • 0
    L
    class Solution(object):
        def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            if matrix == [] or (len(matrix) == 0 or len(matrix[0])==0): return False
            n = len(matrix) - 1
            m = len(matrix[0]) - 1
            j = m
            start = matrix[0][m]
            for i in range(n+1):
                if matrix[i][j] == target: 
                    return True
                elif j>=0  and matrix[i][j] > target:
                    while j>=0 and matrix[i][j] >= target :
                         if matrix[i][j] == target: return True
                         j -= 1
    
            return False
    

Log in to reply
 

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