Skip to content

Commit 43060b1

Browse files
authored
feat(ml): $200.number-of-islands.md (#433)
1 parent 84f471d commit 43060b1

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

problems/200.number-of-islands.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,46 @@ Output: 3
6464

6565
## 代码
6666

67-
* 语言支持:JS, python3,Java
67+
* 语言支持:C++, Java, JS, python3
68+
69+
70+
C++ Code:
71+
72+
```c++
73+
class Solution {
74+
public:
75+
int numIslands(vector<vector<char>>& grid) {
76+
int res = 0;
77+
for(int i=0;i<grid.size();i++)
78+
{
79+
for(int j=0;j<grid[0].size();j++)
80+
{
81+
if(grid[i][j] == '1')
82+
{
83+
dfs(grid, i, j);
84+
res += 1;
85+
}
86+
}
87+
}
88+
return res;
89+
90+
}
91+
void dfs(vector<vector<char>>& grid, int i, int j)
92+
{
93+
// edge
94+
if(i<0 || i>= grid.size() || j<0 || j>= grid[0].size() || grid[i][j] != '1')
95+
{
96+
return;
97+
}
98+
grid[i][j] = '0';
99+
dfs(grid, i+1, j);
100+
dfs(grid, i-1, j);
101+
dfs(grid, i, j+1);
102+
dfs(grid, i, j-1);
103+
}
104+
};
105+
106+
```
68107
69108
Java Code:
70109

0 commit comments

Comments
 (0)