-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
# Java
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
int m = matrix.length, n = matrix[0].length;
int row = 0, col = n - 1;
while(row <= m-1 && col >= 0){
if(target == matrix[row][col]) return true;
else if(target < matrix[row][col]) col--;
else row++;
}
return false;
}
}
# Python
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if len(matrix) == 0 or len(matrix[0]) == 0:
return False
m = len(matrix)
n = len(matrix[0])
row, col = 0, n-1
while row <= m-1 and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] > target:
col -= 1
else:
row += 1
return False
Metadata
Metadata
Assignees
Labels
No labels