# LeetCode 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:

11110110101100000000

Example 2:

11000110000010000011

/**
* Created by kyle on 2016/6/1.
*/
public class NumberOfIsland  {

static  void dfs(char[][] map, int i ,int j){
int m = map.length;
int n = map[0].length;
if(map[i][j] == '0'){
return;
}

if(map[i][j] =='2'){
return;
}

if(map[i][j] == '1'){
map[i][j] = '2';
}

if(i+1 < m){
dfs(map, i+1, j);
}
if(j+1 < n){
dfs(map, i, j+1);
}

if(i-1 >=0){
dfs(map, i-1, j);

}

if( j- 1 >= 0){
dfs(map ,i, j-1);
}

}

static  int numberOfIsland(char[][] grid){

if(grid == null){
return  0;
}
if(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'){
dfs(grid , i ,j );
//  show(grid);

count++;
}
}
}
return  count;
}
static  void show(char[][] islandMap){
System.out.println("island map :");
for(int i =0 ;i< islandMap.length ; i++){
System.out.println(Arrays.toString(islandMap[i]));
}
}

static  public  void main(String[] args){
/*char[][] islandMap = {
{'1','1','1','0','0'},
{'1','1','0','1','0'},
{'0','0','1','0','0'},
{'0','0','0','1','1'},
};*/

char[][] islandMap = {
{'1','1','1'},
{'0','1','0'},
{'1','1','1'}
};

// dfs(islandMap, 0, 0);
// int number = numberOfIsland(new int[0][0]);
int number = numberOfIsland(islandMap);
for(int i =0 ;i< islandMap.length ; i++){
System.out.println(Arrays.toString(islandMap[i]));
}

System.out.println(" number of island : "+ number);
}
}


• 本文已收录于以下专栏：

## leetcode 200 : Number of Islands

leetcode 200 : Number of Islands java c++ python
• xudli
• 2015年05月22日 07:40
• 7351

## Leetcode 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 sur...
• u012614906
• 2017年03月15日 20:47
• 1198

## 200. Number of Islands Leetcode Python

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is sur...
• hyperbolechi
• 2015年04月15日 19:14
• 1631

## 第三周：( LeetCode200 ) Number of Islands（c++）

• marlonlyh
• 2017年03月09日 19:25
• 191

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

• qq508618087
• 2016年03月26日 06:50
• 1960

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

Q: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is s...
• u014616233
• 2016年04月20日 23:23
• 1505

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

• happyaaaaaaaaaaa
• 2016年03月18日 13:02
• 1273

## [leetcode] 200 Number of Islands(DFS)

• NK_test
• 2015年10月12日 23:12
• 2285

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

leetcode解题之200. Number of Islands Java版（岛屿的数量） ，深度优先遍历
• mine_song
• 2017年04月05日 22:07
• 882

## 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 surr...
• gao1440156051
• 2016年08月11日 23:22
• 401

举报原因： 您举报文章：LeetCode 200. Number of Islands 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)