-
Notifications
You must be signed in to change notification settings - Fork 0
/
240.搜索二维矩阵-ii.js
81 lines (81 loc) · 1.52 KB
/
240.搜索二维矩阵-ii.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
/*
* @lc app=leetcode.cn id=240 lang=javascript
*
* [240] 搜索二维矩阵 II
*/
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
let m = 0
let n = 0
let map = new Map()
while (matrix[m] && (matrix[m][n] || matrix[m][n] === 0)) {
if (map.has('' + m + n)) {
return false
}
const proofreading = matrix[m][n]
map.set('' + m + n)
if (proofreading === target) return true
if (proofreading < target) {
if (m === matrix.length - 1) {
n++
continue
}
if (n === matrix[m].length - 1) {
m++
continue
}
if (matrix[m][n + 1] > matrix[m + 1][n]) {
m++
if (map.has('' + m + n)) {
n++
m--
}
while (map.has('' + m + n)) {
n++
}
} else {
n++
if (map.has('' + m + n)) {
m++
n--
}
while (map.has('' + m + n)) {
m++
}
}
} else {
if (m === 0) {
m++
continue
}
if (n === 0) {
n++
continue
}
if (matrix[m][n - 1] > matrix[m - 1][n]) {
n--
if (map.has('' + m + n)) {
n++
m--
}
while (map.has('' + m + n)) {
m--
}
} else {
m--
if (map.has('' + m + n)) {
m++
n--
}
while (map.has('' + m + n)) {
n--
}
}
}
}
return false
}