-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.java
46 lines (38 loc) · 1.19 KB
/
day17.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
45
46
/**
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all surrounded by water.
**/
class Solution {
public int numIslands(char[][] grid) {
int col, row = grid.length;
int i, j, count = 0;
if (row == 0)
return 0;
col = grid[0].length;
for (i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
if(grid[i][j] == '1'){
count++;
merge(grid, i, j);
}
}
}
return count;
}
public void merge(char[][] grid, int i, int j) {
int row = grid.length;
int col = grid[0].length;
if (i < 0 || i >= row)
return;
else if (j < 0 || j >= col)
return;
else if (grid[i][j] != '1')
return;
grid[i][j] = '*';
merge(grid, i-1, j);
merge(grid, i+1, j);
merge(grid, i, j-1);
merge(grid, i, j+1);
}
}