We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0e6cf65 commit e84ebd7Copy full SHA for e84ebd7
200-NumberOfIslands.cpp
@@ -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