python-leetcode-搜索二维矩阵 II
240. 搜索二维矩阵 II - 力扣(LeetCode)
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix or not matrix[0]:
return False
m, n = len(matrix), len(matrix[0])
i, j = 0, n - 1 # 从右上角开始
while i < m and j >= 0:
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
j -= 1 # 左移
else:
i += 1 # 下移
return False