-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoOfIslands.java
executable file
·44 lines (40 loc) · 1.26 KB
/
NoOfIslands.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class NoOfIslands {
public int numIslands(char[][] grid) {
int totalIsland = 0;
char countedAsIsland = '2';
for(int row=0; row<grid.length; row++){
for(int col=0; col< grid[row].length; col++){
if(grid[row][col] == '1'){
dfs(row, col, grid);
totalIsland++;
}
}
}
return totalIsland;
}
private void dfs (int row, int col, char[][] grid){
grid[row][col] = '2';
//up
if(row>0 && grid[row-1][col] == '1')
dfs(row-1, col, grid);
//down
if(row < grid.length-1 && grid[row+1][col]== '1')
dfs(row+1, col, grid);
//left
if( col > 0 && grid[row][col-1] == '1')
dfs(row, col-1, grid);
//right
if(col < grid[row].length-1 && grid[row][col+1] == '1')
dfs(row, col+1, grid);
}
public static void main(String[] args) {
char[][] island = {
{'1','0','1','1', '1'},
{'1','0','1','0', '1'},
{'1','1','1','0', '1'}
};
NoOfIslands noOfIslands = new NoOfIslands();
int k = noOfIslands.numIslands(island);
System.out.println(k);
}
}