# Middle-题目80：200. Number of Islands

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 {
public int numIslands(char[][] grid) {
if (grid.length==0 || grid[0].length==0)
return 0;
int count = 0;
boolean[][] visited = new boolean[grid.length][grid[0].length];
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j]=='1' && !visited[i][j]) {
count++;
dfs(grid, visited, i, j);
}
}
}
return count;
}

private void dfs(char[][] grid, boolean[][] visited, int row, int col) {
if (row<0 || row>=grid.length || col<0 || col>=grid[0].length || visited[row][col] || grid[row][col]=='0') // grid[i][j] is out of range
return;
visited[row][col] = true;
dfs(grid, visited, row-1, col);
dfs(grid, visited, row+1, col);
dfs(grid, visited, row, col-1);
dfs(grid, visited, row, col+1);
}
}

5ms,beats 25.10%，众数3ms,40.17%
Cmershen的碎碎念：

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

2016-04-20 23:23:59

#### leetcode 200 : Number of Islands

2015-05-22 07:40:37

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

2017-04-05 22:07:17

#### leetcode -- Number of Islands -- DFS&BFS重点题

2015-12-16 16:47:02

#### 200. Number of Islands Leetcode Python

2015-04-15 19:14:15

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

2016-04-19 13:13:25

#### [leetcode] 305. Number of Islands II 解题报告

2016-03-26 06:50:25

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

2015-04-10 10:16:24

#### LeetCode 200. Number of Islands（小岛）

2016-05-04 04:23:07

#### leetcode 200. Number of Islands-岛屿数量|并查集算法

2016-03-18 13:02:00