200. Number of Islands
For full Explanation Go to our youtube channel
class Solution {
private void dfs(char [][]a,int i,int j){
if(i<0||i>=a.length||j<0||j>=a[0].length||a[i][j]!='1')
return ;
a[i][j]='*';
dfs(a,i-1,j);//upper
dfs(a,i+1,j);//down
dfs(a,i,j-1);//left
dfs(a,i,j+1);//right
}
public int numIslands(char[][] grid) {
//visited as *
int island=0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]=='1'){
dfs(grid,i,j);
island++;
}
}
}
return island;
}
}
0 Comments