`
SunnyYoona
  • 浏览: 368837 次
社区版块
存档分类
最新评论

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

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

思路

可以参考:Find the number of islands
有一点不同的是本题目只用考虑四个方向,而参考题目是考虑了8个方向。

代码

/*------------------------------------------------------
*   日期:2014-04-10
*   作者:SJF0115
*   题目: 200.Number of Islands
*   网址:https://leetcode.com/problems/number-of-islands/
*   结果:AC
*   来源:LeetCode
*   博客:
--------------------------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int numIslands(vector<vector<char>> &grid) {
        // 行数
        int row = grid.size();
        if(row == 0){
            return 0;
        }//if
        // 列数
        int col = grid[0].size();
        if(col == 0){
            return 0;
        }//if
        int count = 0;
        for(int i = 0;i < row;++i){
            for(int j = 0;j < col;++j){
                // 如果是1且没有被访问过则发现一个新的岛屿
                if(grid[i][j] == '1'){
                    // 以该岛屿做深度遍历
                    DFS(grid,row,col,i,j);
                    ++count;
                }//if
            }//for
        }//for
        return count;
    }
private:
    // grid 地图 row 行数 col 列数 (x,y)当前坐标
    void DFS(vector<vector<char> > &grid,int row,int col,int x,int y){
        if(x < 0 || y < 0 || x >= row || y >= col || grid[x][y] == '0'){
            return;
        }//if
        grid[x][y] = '0';
        // right
        DFS(grid,row,col,x,y+1);
        // left
        DFS(grid,row,col,x,y-1);
        // top
        DFS(grid,row,col,x-1,y);
        // bottom
        DFS(grid,row,col,x+1,y);
    }
};

int main() {
    Solution solution;
    vector<vector<char> > grid =
    {
        {'1', '1', '0', '0', '0'},
        {'1', '1', '0', '0', '0'},
        {'0', '0', '1', '0', '0'},
        {'0', '0', '0', '1', '1'}
    };
    cout<<solution.numIslands(grid)<<endl;
    return 0;
}

运行时间

这里写图片描述

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
分享到:
评论

相关推荐

    leetcode卡-LeetCode:我的LeetCode解决方案

    200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡...

    Leetcode200. 岛屿数量

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/number-of-islands 示例 1: 输入: 11110 11010 11000 00000 输出: 1 示例 2: 输入: 11000 11000 00100 00011 输出: 3 解释: 每座岛屿只能由水平和/...

    LeetCode最全代码

    191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...

    LeetCode解题心得——岛屿数量(python)

    题目 给定一个由 ‘1’(陆地)和 ...链接:https://leetcode-cn.com/problems/number-of-islands 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 用宽度优先搜索思想,将一个根节点(取

    陆地岛屿问题leetcode-number-of-distinct-islands:计算不同岛屿的数量

    狼人问题leetcode 不同岛屿的数量 给定一个由 0 和 1 组成的非空 2D 阵列网格,岛是一组 1(代表陆地)以 4 个方向(水平或垂直)连接。您可以假设网格的所有四个边缘都被水包围。 计算不同岛屿的数量。 当且仅当一...

    扩展矩阵leetcode-leetcode:leetcode

    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. Input: 11000 ...

    leetcode316-LeetCode:leetcode的解决方案

    Islands:DFS My Calendar II:小空间匹配 My Calendar I:同上 *732. My Calendar III:难,小数据量可以用线段匹配,大数据量要用LCT(但是这东西看不懂) Construct String from Binary Tree:中序遍历 Word Ladder...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...

    列表实现岛屿数量(DFS+BFS)

    ** 列表实现岛屿数量(DFS+BFS) ** 给定一个由 ‘1’(陆地)和 ‘0’(水)...链接:https://leetcode-cn.com/problems/number-of-islands 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Global site tag (gtag.js) - Google Analytics