-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path240. Search a 2D Matrix II.js
More file actions
54 lines (45 loc) · 925 Bytes
/
240. Search a 2D Matrix II.js
File metadata and controls
54 lines (45 loc) · 925 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function (matrix, target) {
var mL = matrix.length;
if (mL === 0) {
return false;
}
var nL = matrix[0].length;
var helper = function (target, m, n) {
if (m === mL || n === -1) {
return false;
}
var cur = matrix[m][n];
if (cur === target) {
return true;
}
if (cur > target) {
n -= 1;
}
if (cur < target) {
m += 1;
}
return helper(target, m, n);
};
return helper(target, 0, nL - 1);
};
console.log(searchMatrix([[1, 3, 5]], 5));
console.log(
searchMatrix(
[
[1, 3, 5, 7, 9],
[2, 4, 6, 8, 10],
[11, 13, 15, 17, 19],
[12, 14, 16, 18, 20],
[21, 22, 23, 24, 25],
],
11
)
);
console.log(searchMatrix([[]], 1));
console.log(searchMatrix([[-5]], -10));
console.log(searchMatrix([[1, 1]], 0));