-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch2DMatrix.js
72 lines (64 loc) · 1.58 KB
/
Search2DMatrix.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function searchStringIndexIn2D(strArr, target) {
for (let i = 0; i < strArr.length; i++) {
for (let j = 0; j < strArr[i].length; j++) {
if (strArr[i][j] === target) {
return [i, j];
}
}
}
}
var strArr = [
["hey", "oh"],
["scar", "tissue"],
["other", "side"],
];
let target = "side";
console.log(searchStringIndexIn2D(strArr, target));
//without binary search leetcode
var searchMatrix = function (matrix, target) {
let searchedArray = matrix.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < searchedArray.length; i++) {
for (let j = 0; j < searchedArray[i].length; j++) {
if (searchedArray[i][j] === target) {
return true;
}
}
}
return false;
};
let matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 60],
];
let value = 3;
console.log(searchMatrix(matrix, value));
//binary search
function searchMatrixs(matrix, target) {
let rows = matrix.length;
let cols = matrix[0].length;
let left = 0;
let right = rows * cols - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const midvalue = matrix[Math.floor(mid/cols)][mid % cols];
//The expression Math.floor(mid/cols)
//calculates the row index, and mid % cols calculates the column index.
if (midvalue === target) {
return true;
}
if (target < midvalue) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return false;
}
let s = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 60],
];
let v = 3;
console.log(searchMatrixs(s, v));