Skip to content

Commit 1a7ba4c

Browse files
committed
add solution: number-of-islands
1 parent 1e89b4b commit 1a7ba4c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

number-of-islands/yihyun-kim1.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
var numIslands = function (grid) {
6+
let count = 0;
7+
8+
const dfs = (i, j) => {
9+
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return;
10+
if (grid[i][j] !== "1") return;
11+
12+
grid[i][j] = "0";
13+
14+
dfs(i + 1, j);
15+
dfs(i - 1, j);
16+
dfs(i, j + 1);
17+
dfs(i, j - 1);
18+
};
19+
20+
for (let i = 0; i < grid.length; i++) {
21+
for (let j = 0; j < grid[0].length; j++) {
22+
if (grid[i][j] === "1") {
23+
dfs(i, j);
24+
count++;
25+
}
26+
}
27+
}
28+
29+
return count;
30+
};

0 commit comments

Comments
 (0)