74. Search a 2D Matrix
Leetcode
題目
解答
var searchMatrix = function(matrix, target) {
const m = matrix.length;
const n = matrix[0].length;
let startRow = 0;
let endRow = m - 1;
while(startRow < endRow) {
const mid = Math.ceil((startRow+endRow)/2);
if(matrix[mid][0] > target) {
endRow = mid - 1;
} else {
startRow = mid;
}
}
let startCol = 0;
let endCol = n - 1;
while(startCol < endCol) {
const mid = Math.ceil((startCol+endCol)/2);
if(matrix[startRow][mid] > target) {
endCol = mid - 1;
} else {
startCol = mid;
}
}
return matrix[startRow][startCol] === target;
};測資
Last updated