Skip to content

Commit 439698a

Browse files
committed
fix: update js solution to lcof problem: No.04. FindNumberIn2DArray
close doocs#356
1 parent 956ff27 commit 439698a

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

lcof/面试题04. 二维数组中的查找/README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ class Solution {
8181
* @return {boolean}
8282
*/
8383
var findNumberIn2DArray = function (matrix, target) {
84-
let row = matrix.length;
85-
let col = matrix[0].length;
86-
function dfs(i, j) {
87-
if (i < 0 || j >= col) {
88-
return false;
89-
}
90-
if (matrix[i][j] === target) return true;
91-
else if (matrix[i][j] > target) {
92-
return dfs(i - 1, j);
93-
} else {
94-
return dfs(i, j + 1);
95-
}
84+
let m, n;
85+
if (
86+
matrix == null ||
87+
(m = matrix.length) == 0 ||
88+
matrix[0] == null ||
89+
(n = matrix[0].length) == 0
90+
)
91+
return false;
92+
let i = 0,
93+
j = n - 1;
94+
while (i < m && j >= 0) {
95+
if (matrix[i][j] == target) return true;
96+
if (matrix[i][j] > target) --j;
97+
else ++i;
9698
}
97-
return dfs(row - 1, 0);
99+
return false;
98100
};
99101
```
100102

lcof/面试题04. 二维数组中的查找/Solution.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
* @return {boolean}
55
*/
66
var findNumberIn2DArray = function (matrix, target) {
7-
let row = matrix.length;
8-
let col = matrix[0].length;
9-
function dfs(i, j) {
10-
if (i < 0 || j >= col) {
11-
return false;
12-
}
13-
if (matrix[i][j] === target) return true;
14-
else if (matrix[i][j] > target) {
15-
return dfs(i - 1, j);
16-
} else {
17-
return dfs(i, j + 1);
18-
}
7+
let m, n;
8+
if (
9+
matrix == null ||
10+
(m = matrix.length) == 0 ||
11+
matrix[0] == null ||
12+
(n = matrix[0].length) == 0
13+
)
14+
return false;
15+
let i = 0,
16+
j = n - 1;
17+
while (i < m && j >= 0) {
18+
if (matrix[i][j] == target) return true;
19+
if (matrix[i][j] > target) --j;
20+
else ++i;
1921
}
20-
return dfs(row - 1, 0);
22+
return false;
2123
};

solution/main.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def get_all_questions(self):
8383
headers = {
8484
'accept': 'application/json, text/javascript, */*; q=0.01',
8585
'content-type': 'application/json',
86-
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
87-
'Chrome/77.0.3865.120 Safari/537.36',
86+
'user-agent': LCSpider.user_agent,
8887
'x-requested-with': 'XMLHttpRequest'
8988
}
9089
resp = self.session.get(url='https://leetcode.com/api/problems/all/',
@@ -264,8 +263,8 @@ def remove_empty_folders():
264263
if __name__ == '__main__':
265264
spider = LCSpider()
266265

267-
# spider.get_all_questions()
268-
# spider.save_result()
266+
spider.get_all_questions()
267+
spider.save_result()
269268

270269
spider.generate_readme()
271270
spider.generate_question_readme()

0 commit comments

Comments
 (0)