Search a 2D Matrix II
Question
None
Example 1
Input:
matrix = [
[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]
]
Target = 5
Output:
true
Solution
- ▭
- ▯
all//Search a 2D Matrix II.py
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
row = 0
col = len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if target == matrix[row][col]:
return True
elif target > matrix[row][col]:
row += 1
else:
col -= 1
return False
all//Search a 2D Matrix II.py
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
row = 0
col = len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if target == matrix[row][col]:
return True
elif target > matrix[row][col]:
row += 1
else:
col -= 1
return False