We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1e89b4b commit 1a7ba4cCopy full SHA for 1a7ba4c
1 file changed
number-of-islands/yihyun-kim1.js
@@ -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