LeetCode 200. Number of Islands

原创 2016年06月02日 09:50:34

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

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3



这是一道图论的题。我这儿采用dfs算法,将一个岛的元素全部标志为2,遍历整个map,遇到2和0就跳过,累计没有跳过的次数。

/**
 * 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-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】

【200-Number of Islands(岛的数目)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao...

LeetCode 200. Number of Islands (并查集)

LeetCode 200. Number of Islands (并查集)LeetCode 200 Number of Islands 并查集 问题描述 解题思路 参考代码Tags: * Depth...

第三周:( LeetCode200 ) Number of Islands(c++)

原题:本题是经典的简单图论问题,求岛的个数也即求图上的连通区间的个数。可采用深搜或者广搜。假设共有n个点,时间复杂度为o(n)。本题笔者采用深搜的递归和非递归两种解法。将遍历过的“1”被改为“0”,其...

LeetCode 200. Number of Islands

DFS遍历即可。

leetcode解题之200. Number of Islands Java版(岛屿的数量)

leetcode解题之200. Number of Islands Java版(岛屿的数量) ,深度优先遍历

<LeetCode OJ> 200 / 130 Number of Islands / Surrounded Regions

Total Accepted: 48411 Total Submissions: 171609 Difficulty: Medium Given a 2d grid map of '1's ...

[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...

leetcode Add to List 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...

[LeetCode]200. Number of Islands

[LeetCode]200. Number of Islands题目描述思路BFS深搜,遍历,如果点为1,计数+1,并用深搜的方式将和他连通的为1的点均置0GFS留坑代码BFSclass Soluti...
  • Lcharon
  • Lcharon
  • 2017年03月07日 17:17
  • 3028

LeetCode题解–200. Number of Islands

链接LeetCode题目:https://leetcode.com/problems/number-of-islands/难度:Medium题目 Given a 2d grid map of ‘1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 200. Number of Islands
举报原因:
原因补充:

(最多只允许输入30个字)