`

200 Number of Islands——leetcode

阅读更多

这个是图像中的填充技术,即选择一个种子,然后对其周边联通的的依次填充。

代码未必最快,但很容易理解。

算法复杂度O(M*N)

空间复杂度O(M*N),最坏情况。

算法说明:

<1>初始化 访问标记

<2>对每一个没有访问的cell,进行填充算法

 

填充算法:(使用栈)

<1>设置初始种子,入栈

<2>如果栈空,结束

<3>出栈

依次对周围连通的cell,且没有被填充过,入栈

转2。

 

class Solution {
public:
    int numIslands(vector<vector<char>> &grid) 
    {
        if(grid.empty()||grid[0].empty()){
            return 0;            
        }
        int scc=0;
        const int M=grid.size();
        const int N=grid[0].size();
        vector<vector<int> > visited(M);
        for(int i=0;i<M;i++)
        {
            visited[i].resize(N);
        }
        vector<pair<int,int> > s;
        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {
                if(!visited[i][j] && grid[i][j]=='1')
                {
                    s.clear();
                    visited[i][j]=1;
                    s.push_back(pair<int,int>(i,j));
                    while(!s.empty())
                    {
                        pair<int,int> pr = s.back();
                        s.pop_back();

                        int x = pr.first;
                        int y = pr.second;

                        x=pr.first-1;
                        if(x>=0&&grid[x][y]=='1' && !visited[x][y])
                        {
                            visited[x][y]=1;
                            s.push_back(pair<int,int>(x,y));

                        }
                        x=pr.first+1;
                        if(x<M&&grid[x][y]=='1' && !visited[x][y])
                        {
                            visited[x][y]=1;
                            s.push_back(pair<int,int>(x,y));
                        }
                        x=pr.first;
                        y=pr.second-1;
                        if(y>=0&&grid[x][y]=='1' && !visited[x][y])
                        {
                            visited[x][y]=1;
                            s.push_back(pair<int,int>(x,y));
                        }
                        y=pr.second+1;
                        if(y<N&&grid[x][y]=='1' && !visited[x][y])
                        {
                            visited[x][y]=1;
                            s.push_back(pair<int,int>(x,y));
                        }
                    }
                    ++scc;
                }
            }
        }
        return scc;
    }
};

 

分享到:
评论

相关推荐

    python-leetcode题解之200-Number-of-Islands.py

    python python_leetcode题解之200_Number_of_Islands.py

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

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

    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 打卡...

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

    扩展矩阵leetcode-leetcode:leetcode

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

    leetcode316-LeetCode:leetcode的解决方案

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

    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各公司题目合集

    Number of Islands(岛屿数量),这类问题通常与图的遍历算法有关;LRU Cache(最近最少使用缓存),这需要了解缓存淘汰算法并实现相关的数据结构。 综上所述,LeetCode上的题目涵盖了计算机科学中的众多重要知识点...

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

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

    The effect of a guide field on the structures of magnetic islands formed during multiple X line reconnection: two-dimensional particle-in-cell simulations

    在探讨标题“引导场对重联磁岛内磁场结构的影响:二维全粒子模拟”所含的知识点之前,首先需要理解文档中的一些专业术语和概念。 磁岛(Magnetic Island)是磁场重联过程中的一个现象,指的是在等离子体中的磁场线...

    春节7天练丨Day6:图1

    - **Number of Islands**:这个问题要求计算二维数组(可以视为网格)中连通的“1”(代表陆地)组成的岛屿数量。 - **Valid Sudoku**:检查一个数独是否有效,即每一行、每一列以及每一个宫格内的数字是否都在1到...

    leetcode:LeetCode解决方案

    10. **图论**:包括深度优先搜索(DFS)和广度优先搜索(BFS),例如"岛屿数量"(Number of Islands)要求计算二维矩阵中的连通岛屿。 Python 作为一种强大且易学的语言,是解决这些问题的常用工具,其简洁的语法和...

    LeetCode:LeetCode刷题

    例如,"岛屿数量"(Number of Islands)问题,找出二进制矩阵中的连通岛屿。 7. **动态规划(Dynamic Programming)**:动态规划是解决复杂问题的有效方法,通过将问题分解为子问题来求解。例如,"背包问题"...

    Ballari-2016-UAV MONITORING FOR ENVIROMENTAL MANAGEMENT.pdf

    In the Galapagos Islands, where 97% of the territory is protected and ecosystem dynamics are highly vulnerable, timely and accurate information is key for decision making. An appropriate monitoring ...

    Preliminary Report of the Appearance in the Philippine

    Preliminary Report of the Appearance in the Philippine Islands of a Disease Clinically Resembling Glanders

    简洁的艺术+——+astro+带来的全新体验.pdf

    标题中的“简洁的艺术+——+astro+带来的全新体验”指的是Astro这个技术框架为Web开发带来的一种新的、注重简洁和高效的设计理念。Astro是一款新兴的构建静态网站的工具,它旨在通过最小化JavaScript的使用,提高...

    cet-62015年6月-2017年12月6级翻译真题及多版译本

    On the western side of the lake are the well-known “Bird Islands", which attract birdwatchers from across the globe. Every summer sees numerous visitors come here to watch the Qinghai Lake ...

Global site tag (gtag.js) - Google Analytics