-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumIslands.cpp
More file actions
29 lines (26 loc) · 739 Bytes
/
Copy pathnumIslands.cpp
File metadata and controls
29 lines (26 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
void dfs(int i , int j , int n , int m ,vector<vector<char>>& grid){
if(i<0 || j<0 || i>=n || j>=m || grid[i][j] =='0' )return ;
grid[i][j]='0';
dfs(i+1,j,n,m,grid);
dfs(i-1,j,n,m,grid);
dfs(i,j-1,n,m,grid);
dfs(i,j+1,n,m,grid);
}
int numIslands(vector<vector<char>>& grid) {
int n = grid.size();
if(n==0) return 0;
int m = grid[0].size();
int nIslands =0;
for(int i =0 ; i<n;i++){
for(int j =0 ; j<m;j++){
if(grid[i][j]=='1'){
nIslands++;
dfs(i,j,n,m,grid);
}
}
}
return nIslands;
}
};