-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path200.number-of-islands.java
More file actions
47 lines (39 loc) · 1.03 KB
/
Copy path200.number-of-islands.java
File metadata and controls
47 lines (39 loc) · 1.03 KB
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
// @lc code=start
class Solution {
public int numIslands(char[][] grid) {
// edge
if (grid == null || grid.length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
int island = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
island++;
dfs(grid, i, j);
}
}
}
return island;
}
private void dfs(char[][] grid, int i, int j) {
// check boundaries and if curr is land
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') {
return;
}
// make as visited
grid[i][j] = '0';
dfs(grid, i - 1, j); // top
dfs(grid, i + 1, j); // bottom
dfs(grid, i, j - 1); // left
dfs(grid, i, j + 1); // right
}
}
// @lc code=end
/*
* @lc app=leetcode id=200 lang=java
*
* [200] Number of Islands
*/