[Python手撕]搜尋二維矩陣

Duancf發表於2024-09-20
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:

        n = len(matrix)
        m = len(matrix[0])

        row = 0
        col = m-1

        while row <= n-1 and col >= 0:
            if matrix[row][col] == target:
                return True
            elif matrix[row][col] > target:
                col -= 1
            else:
                row += 1
        
        return False
 
        

相關文章