Skip to content

Commit

Permalink
Create 200-Number-of-Islands.java
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Jul 6, 2022
1 parent f67e6b0 commit 21f3182
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions java/200-Number-of-Islands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int numIslands(char[][] grid) {
int count = 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);
count++;
}
}
}
return count;
}

public void dfs(char[][] grid, int i, int j) {
if (i<0 || j<0 || i>= grid.length || j>=grid[0].length || grid[i][j]=='0') {
return;
}
grid[i][j] = '0';
dfs(grid, i+1, j);
dfs(grid, i, j+1);
dfs(grid, i-1, j);
dfs(grid, i, j-1);
}
}

0 comments on commit 21f3182

Please sign in to comment.