Skip to content

Commit 2b714ff

Browse files
committed
⏪ [200] Rewinding ...
1 parent f1dce95 commit 2b714ff

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

200/o_solution.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,54 @@ function numIslands(grid) {
66

77
let count = 0;
88

9-
const dfs = (i, j) => {
9+
const dfs = (row, col) => {
1010
if (
11-
i < 0 ||
12-
i >= grid.length ||
13-
j < 0 ||
14-
j >= grid[i].length ||
15-
grid[i][j] === "0"
11+
row < 0 ||
12+
row >= grid.length ||
13+
col < 0 ||
14+
col >= grid[row].length ||
15+
grid[row][col] === "0"
1616
) {
1717
return;
1818
}
1919

20-
grid[i][j] = "0";
20+
grid[row][col] = "0";
2121

22-
console.log("i, j")
23-
console.log(i, j)
22+
console.log("row, col")
23+
console.log(row, col)
2424

25-
dfs(i + 1, j);
26-
dfs(i - 1, j);
27-
dfs(i, j + 1);
28-
dfs(i, j - 1);
25+
// y=col, x=row
26+
// t=y-1
27+
// r=x+1
28+
// b=y+1
29+
// l=x-1
30+
dfs(row + 1, col);
31+
dfs(row - 1, col);
32+
dfs(row, col + 1);
33+
dfs(row, col - 1);
2934
};
3035

31-
for (let i = 0; i < grid.length; i++) {
32-
for (let j = 0; j < grid[i].length; j++) {
33-
if (grid[i][j] === "1") {
36+
for (let row = 0; row < grid.length; row++) {
37+
for (let col = 0; col < grid[row].length; col++) {
38+
if (grid[row][col] === "1") {
3439
count++;
35-
dfs(i, j);
40+
dfs(row, col);
3641
}
3742
}
3843
}
3944

45+
console.log("count")
46+
console.log(count)
47+
4048
return count;
4149
}
4250

43-
numIslands([
44-
["1", "1", "0", "0", "0"],
45-
["1", "1", "0", "0", "0"],
46-
["1", "0", "1", "0", "0"],
47-
["1", "0", "0", "1", "1"]
48-
]);
51+
// numIslands([
52+
// ["1", "1", "0", "0", "0"],
53+
// ["1", "1", "0", "0", "0"],
54+
// ["1", "0", "1", "0", "0"],
55+
// ["1", "0", "0", "1", "1"]
56+
// ]);
4957

5058
// numIslands([
5159
// ["1", "1", "1"],

0 commit comments

Comments
 (0)