Skip to content

feat: add js solution to lc problem: No.1905 #3453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion solution/1900-1999/1905.Count Sub Islands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
<p><strong>示例 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/testcasex2.png" style="width: 491px; height: 201px;">
<pre><b>输入:</b>grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
<b>输出:</b>2
<b>输出:</b>2
<strong>解释:</strong>如上图所示,左边为 grid1 ,右边为 grid2 。
grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。
</pre>
Expand Down Expand Up @@ -220,6 +220,35 @@ function countSubIslands(grid1: number[][], grid2: number[][]): number {
}
```

#### JavaScript

```js
function countSubIslands(grid1, grid2) {
const [m, n] = [grid1.length, grid1[0].length];
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j) => {
let ok = grid1[i][j];
grid2[i][j] = 0;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) {
ok &= dfs(x, y);
}
}
return ok;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; j++) {
if (grid2[i][j]) {
ans += dfs(i, j);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
31 changes: 30 additions & 1 deletion solution/1900-1999/1905.Count Sub Islands/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The 1s colored red in grid2 are those considered to be part of a sub-island. The
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1905.Count%20Sub%20Islands/images/testcasex2.png" style="width: 491px; height: 201px;" />
<pre>
<strong>Input:</strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
<strong>Output:</strong> 2
<strong>Output:</strong> 2
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
</pre>
Expand Down Expand Up @@ -220,6 +220,35 @@ function countSubIslands(grid1: number[][], grid2: number[][]): number {
}
```

#### JavaScript

```js
function countSubIslands(grid1, grid2) {
const [m, n] = [grid1.length, grid1[0].length];
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j) => {
let ok = grid1[i][j];
grid2[i][j] = 0;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) {
ok &= dfs(x, y);
}
}
return ok;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; j++) {
if (grid2[i][j]) {
ans += dfs(i, j);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
24 changes: 24 additions & 0 deletions solution/1900-1999/1905.Count Sub Islands/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function countSubIslands(grid1, grid2) {
const [m, n] = [grid1.length, grid1[0].length];
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i, j) => {
let ok = grid1[i][j];
grid2[i][j] = 0;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y]) {
ok &= dfs(x, y);
}
}
return ok;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; j++) {
if (grid2[i][j]) {
ans += dfs(i, j);
}
}
}
return ans;
}
Loading