LeetCode(200) Number of Islands

原创 2015年11月20日 13:21:59

题目

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.

分析

题目描述抽象为一个图的问题,题目本质便是求连通子图的数目。

借助图的遍历算法DFS的思想,遍历该二维矩阵,每当遇到一个‘1’计数增一,同时以该坐标为起点dfs该矩阵把相邻坐标为‘1’的元素改为非1;最终计数的结果即是连通子图数量。

AC代码

class Solution {
public:
    //等价于计算连通子图的个数
    int numIslands(vector<vector<char>>& grid) {
        if (grid.empty())
            return 0;

        //计算该二维数组的行列
        int rows = grid.size();
        int cols = grid[0].size();

        int count = 0;
        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                if (grid[i][j] == '1')
                {
                    ++count;
                    dfs(grid, i, j);
                }
                continue;
            }//for
        }//for
        return count;
    }

    void dfs(vector<vector<char>> &grid, int r, int c)
    {
        if (grid.empty())
            return;

        //计算该二维数组的行列
        int rows = grid.size();
        int cols = grid[0].size();

        if (r < 0 || r >= rows || c < 0 || c >= cols)
            return;

        if (grid[r][c] == '1')
        {
            //改变当前元素值为非'1'
            grid[r][c] = '2';
            dfs(grid, r, c + 1);
            dfs(grid, r + 1, c);
            dfs(grid, r, c - 1);
            dfs(grid, r - 1, c);
        }//if
        return;
    }
};

GitHub测试程序源码

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【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

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2016年12月30日 21:11
  • 115

(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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode(200) Number of Islands
举报原因:
原因补充:

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