# [Leetcode]Number of Island

[题目]

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.

Example 1:

11110
11010
11000
00000

Example 2:

11000
11000
00100
00011

[思路]

[代码]

public class Solution {
private static final int[][] DIRS = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
private static final char L = '1', W = '0';

public void dfsFill(char[][] g, int x, int y) {
if (x >= 0 && x < g.length && y >= 0 && y < g[0].length && g[x][y] == L) {
g[x][y] = W;
for (int[] d : DIRS) dfsFill(g, x + d[0], y + d[1]);
}
}

public int numIslands(char[][] grid) {
int num = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == L) {
num++;
dfsFill(grid, i, j);
}
}
}
return num;
}
}

#### leetcode 200 : Number of Islands

2015-05-22 07:40:37

#### 【LeetCode】Number of Islands 解题报告

2015-04-10 10:16:24

#### LeetCode 305. Number of Islands II（小岛）

2016-04-19 13:13:25

#### Leetcode 200 Number of Islands 并查集

2017-03-15 20:47:11

#### (LeetCode 200)Number of Islands(并查集、DFS)

2016-04-20 23:23:59

#### LeetCode695:Max Area of Island 解答

2017-10-16 15:59:38

#### leetcode解题之200. Number of Islands Java版（岛屿的数量）

2017-04-05 22:07:17

#### 694. Number of Distinct Islands

2017-10-08 16:01:01

#### 200. Number of Islands Leetcode Python

2015-04-15 19:14:15

#### leetcode 695(Max Area of Island)

2017-10-09 15:57:19