-
Notifications
You must be signed in to change notification settings - Fork 33
/
Number_of_Islands.java
35 lines (29 loc) · 978 Bytes
/
Number_of_Islands.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
// Leetcode 200. Number of Islands
// Question - https://leetcode.com/problems/number-of-islands/
class Solution {
public int numIslands(char[][] grid) {
int ans = 0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]=='1'){
ans++;
dfs(grid,i,j);
}
}
}
return ans;
}
void dfs(char[][] grid, int x, int y){
int rows = grid.length;
int cols = grid[0].length;
//check for validity of x and y
//continue DFS only if the current cell holds a value '1'
if(x<0 || y<0 || x>=rows || y>=cols || grid[x][y]!='1') return;
//make changes inplace instead of using a separate array to save memory
grid[x][y] = '*';
dfs(grid,x+1,y);
dfs(grid,x-1,y);
dfs(grid,x,y+1);
dfs(grid,x,y-1);
}
}