1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: n = len(matrix) if n == 0: return False m = len(matrix[0]) left = 0 right = m * n - 1 while left <= right: index = (right + left) // 2 row = index // m col = index % m if matrix[row][col] > target: right = index - 1 elif matrix[row][col] < target: left = index + 1 else: return True else: return False
|