forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200_Number_of_islands
79 lines (67 loc) · 1.95 KB
/
200_Number_of_islands
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// https://leetcode.com/problems/number-of-islands/
class Solution {
public int numIslands(char[][] grid) {
int count = 0;
for(int i=0;i<grid.length;i++)
{
for(int j=0;j<grid[i].length;j++)
{
if(grid[i][j]=='1')
{
count+=1;
callBFS(grid,i,j);
}
}
}
return count;
}
public void callBFS(char[][] grid,int i,int j){
if(i<0 || i>=grid.length || j<0 || j>=grid[i].length || grid[i][j]=='0')
return;
grid[i][j] = '0';
callBFS(grid,i+1,j);
callBFS(grid,i-1,j);
callBFS(grid,i,j-1);
callBFS(grid,i,j+1);
}
}
// int adjacent = 0;
// int islands = 0;
// HashMap<Integer,List<Integer>> row = new HashMap<Integer,ArrayList<Integer>>();
// HashMap<Integer,List<Integer>> col = new HashMap<Integer,ArrayList<Integer>>();
// int pos = 1;
// for(int i=0;i<grid.length;i++)
// {
// for(int j=0;j<grid.length;j++)
// {
// if(grid[i][j]=='1' && row.containsValue())
// }
// }
// }
// if(grid==null || grid.length==0||grid[0].length==0)
// return 0;
// int m = grid.length;
// int n = grid[0].length;
// int count=0;
// for(int i=0; i<m; i++){
// for(int j=0; j<n; j++){
// if(grid[i][j]=='1'){
// count++;
// merge(grid, i, j);
// }
// }
// }
// return count;
// }
// public void merge(char[][] grid, int i, int j){
// int m=grid.length;
// int n=grid[0].length;
// if(i<0||i>=m||j<0||j>=n||grid[i][j]!='1')
// return;
// grid[i][j]='X';
// merge(grid, i-1, j);
// merge(grid, i+1, j);
// merge(grid, i, j-1);
// merge(grid, i, j+1);
// }
// }