zoukankan      html  css  js  c++  java
  • 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量。一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成。你可以假设网格的四个边均被水包围。
    示例 1:
    11110
    11010
    11000
    00000
    答案: 1
    示例 2:
    11000
    11000
    00100
    00011
    答案: 3

    详见:https://leetcode.com/problems/number-of-islands/description/

    Java实现:

    class Solution {
        public int numIslands(char[][] grid) {
            int m=grid.length;
            if(m==0||grid==null){
                return 0;
            }
            int res=0;
            int n=grid[0].length;
            boolean[][] visited=new boolean[m][n];
            for(int i=0;i<m;++i){
                for(int j=0;j<n;++j){
                    if(grid[i][j]=='1'&&!visited[i][j]){
                        numIslandsDFS(grid,visited,i,j);
                        ++res;
                    }
                }
            }
            return res;
        }
        private void numIslandsDFS(char[][] grid,boolean[][] visited,int x,int y){
            if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != '1' || visited[x][y]){
                return;
            }
            visited[x][y] = true;
            numIslandsDFS(grid, visited, x - 1, y);
            numIslandsDFS(grid, visited, x + 1, y);
            numIslandsDFS(grid, visited, x, y - 1);
            numIslandsDFS(grid, visited, x, y + 1);
        }
    }
    

    C++实现:

    class Solution {
    public:
        int numIslands(vector<vector<char> > &grid) {
            if (grid.empty() || grid[0].empty())
            {
                return 0;
            }
            int m = grid.size(), n = grid[0].size(), res = 0;
            vector<vector<bool> > visited(m, vector<bool>(n, false));
            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (grid[i][j] == '1' && !visited[i][j])
                    {
                        numIslandsDFS(grid, visited, i, j);
                        ++res;
                    }
                }
            }
            return res;
        }
        void numIslandsDFS(vector<vector<char> > &grid, vector<vector<bool> > &visited, int x, int y) {
            if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] != '1' || visited[x][y])
            {
                return;
            }
            visited[x][y] = true;
            numIslandsDFS(grid, visited, x - 1, y);
            numIslandsDFS(grid, visited, x + 1, y);
            numIslandsDFS(grid, visited, x, y - 1);
            numIslandsDFS(grid, visited, x, y + 1);
        }
    };
    
  • 相关阅读:
    [Leetcode]7. 整数反转
    [Leetcode]6. Z 字形变换
    [Leetcode]5. 最长回文子串
    java实现各种排序算法1
    [Leetcode]4. 寻找两个正序数组的中位数
    css设置字体单行,多行超出省略号显示
    如何获得select被选中option的value和text和......
    在vue项目中,将juery设置为全局变量
    js中遍历对象的属性和值的方法
    深入理解JS的事件绑定、事件流模型
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8745813.html
Copyright © 2011-2022 走看看