-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path074.js
More file actions
32 lines (27 loc) · 697 Bytes
/
074.js
File metadata and controls
32 lines (27 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
const rowCount = matrix.length;
if (rowCount === 0) return false;
const colCount = matrix[0].length;
if (colCount === 0) return false;
if (matrix[0][0] > target) return false;
let row = 0;
while(true) {
if (matrix[row][0] === target) { return true; }
if (row === rowCount - 1) break;
if (matrix[row + 1][0] <= target) {
++row;
} else {
break;
}
}
for (let col = 0; col < colCount; ++col) {
if (matrix[row][col] === target) return true;
if (matrix[row][col] > target) return false;
}
return false;
};