Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 730 Bytes

File metadata and controls

41 lines (30 loc) · 730 Bytes

240 Search a 2D Matrix II

Description

link


Solution

Bi Binary Search


Code

O(m + n)

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