240 Search a 2D Matrix II
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if len(matrix) == 0 or (len(matrix) == 1 and len(matrix[0]) == 0):
return False
rows = len(matrix)
cols = len(matrix[0])
r = 0
c = cols - 1
while c >= 0 and r < rows:
if matrix[r][c] < target:
r += 1
elif matrix[r][c] > target:
c -= 1
else:
return True
return FalseLast updated
