-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
134 lines (132 loc) · 3.55 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* @lc app=leetcode id=240 lang=javascript
*
* [240] Search a 2D Matrix II
*
* https://leetcode.com/problems/search-a-2d-matrix-ii/description/
*
* algorithms
* Medium (40.11%)
* Total Accepted: 159.1K
* Total Submissions: 396.1K
* Testcase Example: '[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\n5'
*
* Write an efficient algorithm that searches for a value in an m x n matrix.
* This matrix has the following properties:
*
*
* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.
*
*
* Example:
*
* Consider the following matrix:
*
*
* [
* [1, 4, 7, 11, 15],
* [2, 5, 8, 12, 19],
* [3, 6, 9, 16, 22],
* [10, 13, 14, 17, 24],
* [18, 21, 23, 26, 30]
* ]
*
*
* Given target = 5, return true.
*
* Given target = 20, return false.
*
*/
/**
* 思路:
*
* 遍历每一行,看看最大值和最小值是否能包含target,如果能包含则对其用二分查找
* 不能则略过
*
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
let i = 0;
while (i < matrix.length) {
const row = matrix[i];
if (row[0] === target || row[row.length - 1] === target) {
return true;
}
if (row[0] < target && row[row.length - 1] > target) {
if (foundTarget(row, target)) {
return true;
}
}
i++;
}
return false;
function foundTarget(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const midNum = arr[mid];
if (midNum > target) {
right = mid - 1;
continue;
}
if (midNum < target) {
left = mid + 1;
continue;
}
return true;
}
return false;
}
};
/**
*
* 思路二:
* 由于数组的特性,我们可以从二维数组的右上角出发,如果目标小于该数,
* 则向左移动(左边的数字肯定更小,而当前列中所有数字都比该数字大)。
* 如果该数比目标小,则向下移动(下边的数字肯定更大,而当前行的所有数字逗比该数字小)。
* 这样反复重复该过程就能找到目标数。如果直到左下角都没有该数,说明找不到该数。同样的,这题也可以从左下角向右上角寻找。
*/
/**
*
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
searchMatrix = function(matrix, target) {
if (matrix.length === 0 || matrix[0].length === 0) {
return false;
}
let startX = matrix[0].length - 1;
let startY = 0;
while (startY < matrix.length && startX >= 0) {
const curNum = matrix[startY][startX];
if (target < curNum) {
startX --;
continue;
}
if (target > curNum) {
startY++;
continue;
}
return true;
}
return false;
}
const test =
[ [1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]];
console.log(searchMatrix(test, 20));
console.log(searchMatrix([[-5]], -10));
module.exports = {
id:'240',
title:'Search a 2D Matrix II',
url:'https://leetcode.com/problems/search-a-2d-matrix-ii/description/',
difficulty:'Medium',
}