Open Tcdian opened 3 years ago
function searchMatrix(matrix: number[][], target: number): boolean {
if (_.isEmpty(matrix) || _.isEmpty(_.first(matrix))) {
return false;
}
let x = 0;
let y = matrix[0].length - 1;
while (x < matrix.length && y >= 0) {
if (matrix[x][y] < target) {
x++;
} else if (matrix[x][y] > target) {
y--;
} else {
return true;
}
}
return false;
};
74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an
m x n
matrix. This matrix has the following properties:Example 1
Example 2
Example 3
Constraints
m == matrix.length
n == matrix[i].length
0 <= m, n <= 100
matrix[i][j], target
<= 104