Skip to content

Commit 1801335

Browse files
committed
add js solution for leetcode 65 133 417
1 parent 3a13e97 commit 1801335

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
|438|[Find all Anagrams in a string](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | |Medium|
9999
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/) | |Medium|
100100
|418|[SentenceScreenFitting](https://leetcode.com/problems/sentence-screen-fitting/) ♥ | |Easy|
101+
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [js](./algorithms/pacificAtlanticWaterFlow/pacificAtlanticWaterFlow.js) |Medium|
101102
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | |Medium|
102103
|415|[Add Strings](https://leetcode.com/problems/add-strings/) | |Easy|
103104
|414|[Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | |Easy|
@@ -298,7 +299,7 @@
298299
|136|[Single Number](https://leetcode.com/problems/single-number/)| [java](./algorithms/singleNum/Solution.java) |Medium|
299300
|135|[Candy](https://leetcode.com/problems/candy/)| |Hard|
300301
|134|[Gas Station](https://leetcode.com/problems/gas-station/)| |Medium|
301-
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)| |Medium|
302+
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)| [js](./algorithms/cloneGraph/cloneGraph.js) |Medium|
302303
|132|[Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/)| |Hard|
303304
|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| |Medium|
304305
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| |Medium|
@@ -366,7 +367,7 @@
366367
|68|[Text Justification](https://leetcode.com/problems/text-justification/)| |Hard|
367368
|67|[Add Binary](https://leetcode.com/problems/add-binary/)| [java](./algorithms/addBinary/Solution.java) |Easy|
368369
|66|[Plus One](https://leetcode.com/problems/plus-one/)| |Easy|
369-
|65|[Valid Number](https://leetcode.com/problems/valid-number/)| |Easy|
370+
|65|[Valid Number](https://leetcode.com/problems/valid-number/)| [js](./algorithms/validNumber/validNumber.js) |Easy|
370371
|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| |Medium|
371372
|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)| |Medium|
372373
|62|[Unique Paths](https://leetcode.com/problems/unique-paths/)| |Medium|

algorithms/cloneGraph/cloneGraph.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var cloneGraph = function (node) {
2+
if (!node) return;
3+
const visited = new Map();
4+
const dfs = (n) => {
5+
const nCopy = new Node(n.val);
6+
visited.set(n, nCopy);
7+
(n.neighbors || []).forEach(ne => {
8+
if (!visited.has(ne)) {
9+
dfs(ne);
10+
}
11+
nCopy.neighbors.push(visited.get(ne));
12+
})
13+
};
14+
dfs(node);
15+
return visited.get(node);
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var pacificAtlantic = function (matrix) {
2+
if (!matrix || !matrix[0]) return [];
3+
4+
const m = matrix.length;
5+
const n = matrix[0].length;
6+
const flow1 = Array.from({ length: m }, () => new Array(n).fill(false));
7+
const flow2 = Array.from({ length: m }, () => new Array(n).fill(false));
8+
9+
const dfs = (r, c, flow) => {
10+
flow[r][c] = true;
11+
[[r - 1, c], [r + 1, c], [r, c - 1], [r, c + 1]].forEach(([nr, nc]) => {
12+
if (
13+
// 保证在矩阵中
14+
nr >= 0 && nr < m &&
15+
nc >= 0 && nc < n &&
16+
// 防止死循环
17+
!flow[nr][nc] &&
18+
// 保证逆流而上
19+
matrix[nr][nc] >= matrix[r][c]
20+
) {
21+
dfs(nr, nc, flow);
22+
}
23+
})
24+
};
25+
26+
// 沿着海岸线逆流而上
27+
for (let r = 0; r < m; r++) {
28+
dfs(r, 0, flow1)
29+
dfs(r, n - 1, flow2);
30+
}
31+
for (let c = 0; c < n; c++) {
32+
dfs(0, c, flow1);
33+
dfs(m - 1, c, flow2);
34+
}
35+
36+
// 收集能流到两个大洋里的坐标
37+
const res = [];
38+
for(let r = 0; r < m; r++) {
39+
for(let c = 0; c < n; c++) {
40+
if(flow1[r][c] && flow2[r][c]) {
41+
res.push([r, c])
42+
}
43+
}
44+
}
45+
return res;
46+
};

algorithms/validNumber/validNumber.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var isNumber = function (s) {
2+
/**
3+
* blank 空格
4+
* sign + -
5+
* digit 0-9
6+
* e e
7+
* . .
8+
*/
9+
const graph = {
10+
0: { blank: 0, sign: 1, ".": 2, digit: 6 },
11+
1: { digit: 6, ".": 2 },
12+
2: { digit: 3 },
13+
3: { digit: 3, e: 4 },
14+
4: { digit: 5, sign: 7 },
15+
5: { digit: 5 },
16+
6: { digit: 6, ".": 3, e: 4 },
17+
7: { digit: 5 },
18+
};
19+
20+
let state = 0;
21+
22+
for (let c of s.trim()) {
23+
if (c >= '0' && c <= '9') {
24+
c = 'digit';
25+
} else if (c === ' ') {
26+
c = 'blank';
27+
} else if (c === '+' || c === '-') {
28+
c = 'sign';
29+
}
30+
// 如果得到的当前节点为 undefined 说明不在可用字符范围,直接返回false
31+
if (graph[state] === undefined) return false;
32+
state = graph[state][c];
33+
// 如果得到的状态为 undefined 说明不在可用字符范围,也直接返回false
34+
if (state === undefined) return false;
35+
}
36+
37+
// 走到红色节点就为true
38+
if ([3, 5, 6].includes(state)) {
39+
return true;
40+
}
41+
return false;
42+
};

0 commit comments

Comments
 (0)