Skip to content

Commit e84ebd7

Browse files
committed
Create 200-NumberOfIslands.cpp
1 parent 0e6cf65 commit e84ebd7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

200-NumberOfIslands.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
4+
void dfs(std::vector<std::vector<char>>& grid, int row, int col){
5+
if(row < 0 || row >= grid.size()){return;}
6+
if(col < 0 || col >= grid[0].size()){return;}
7+
if(grid[row][col] != '1'){return;}
8+
grid[row][col] = 'x';
9+
dfs(grid, row - 1, col);
10+
dfs(grid, row + 1, col);
11+
dfs(grid, row, col - 1);
12+
dfs(grid, row, col + 1);
13+
}
14+
15+
int numIslands(vector<vector<char>>& grid) {
16+
17+
int nr = grid.size();
18+
int nc = grid[0].size();
19+
int cnt(0);
20+
21+
for(int row = 0; row < nr; row++){
22+
for(int col = 0; col < nc; col++){
23+
if(grid[row][col] == '1'){
24+
++cnt;
25+
dfs(grid, row, col);
26+
}
27+
}
28+
}
29+
30+
return cnt;
31+
}
32+
};

0 commit comments

Comments
 (0)