Gary Lai
  • Home
  • About
Sign in Subscribe

240. Search a 2D Matrix II

Last updated on  Aug 14, 2024

240. Search a 2D Matrix II

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        rows = len(matrix)
        cols = len(matrix[0])
    
        i = 0
        j = cols - 1
        while i < rows and j >= 0:
            if matrix[i][j] == target:
                return True
            if matrix[i][j] < target:
                i += 1
            else:
                j -= 1
        return False

類似題目 74. Search a 2D Matrix

Previous 74. Search a 2D Matrix
Next 870. Advantage Shuffle
Gary Lai © 2025
  • Sign up
Powered by Ghost