200. Number of Islands+130. Surrounded Regions(并查集/DFS)

【AUCell打分】:评估一个基因集在单细胞转录组的每个细胞中特定的活性程度 AUCell使用曲线下面积来计算输入基因集的一个有意义的基因子集是否在每个细胞的表达基因中富集。AUC 分数在所有细胞中的分布允许探索特征的相对表达。由于评分方法是基于排名的,因此 AUCell 与基因表达单位和归一化程序无关。此外,由于细胞是单独评估的,因此可以很容易地应用于更大的数据集。 阅读详情

首先看200题:

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

DFS:

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if(grid.empty())
            return 0;
        m = grid.size();
        n = grid[0].size();
        int counter = 0;
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j] == '1'){
                    dfs_mark(grid, i, j);
                    ++counter;
                }
            }
        }
        return counter;
    }
    void dfs_mark(vector<vector<char>>& grid, int x, int y){
        if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != '1')
            return;
        grid[x][y] = '0';
        dfs_mark(grid, x-1, y);
        dfs_mark(grid, x+1, y);
        dfs_mark(grid, x, y-1);
        dfs_mark(grid, x, y+1);
    }
private:
    int m;
    int n;
};

并查集版本:

class Solution {
public:
    class uf_set {
    public:
        uf_set(const vector<vector<char>>& grid, int m, int n) 
            : father_(new int[m*n]), rnk_(new int[m*n]), counter_(0){
                fill(rnk_, rnk_+m*n, 0);
                for(int i=0; i<m; ++i){
                    for(int j=0; j<n; ++j){
                        if(grid[i][j] == '1'){
                            int id = i * n + j;
                            father_[id] = id;
                            ++counter_;
                        }
                    }
                }
        }
        ~uf_set(){
            delete []father_;
            delete []rnk_;
        }
    public:
        int find(int x){
            int root = x;
            while(root != father_[root])
                root = father_[root];

            while(x != father_[x]){
                int y = father_[x];
                father_[x] = root;
                x = y;
            }   
            return root;
        }
        void un(int x, int y){
            x = find(x), y = find(y);
            if(x == y) return ;
            if(rnk_[x] > rnk_[y]) father_[y] = x;
            else father_[x] = y, rnk_[y] += rnk_[x] == rnk_[y];
            --counter_;
        }
        int counter() const { return counter_; }

    private:
        int* father_;
        int* rnk_;
        int  counter_;
    };
public:
    vector<vector<char>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int numIslands(vector<vector<char>>& grid) {
        if(grid.empty())
            return 0;
        int m = grid.size(), n = grid[0].size();
        uf_set us(grid, m, n);
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(grid[i][j] == '1'){
                    for(auto d: direction){
                        int x = i + d[0];
                        int y = j + d[1];
                        if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1'){
                            int id1 = i * n + j;
                            int id2 = x * n + y;
                            us.un(id1, id2);
                        }
                    }
                }
            }
        }
        int res = us.counter();
        return res;
    }
};

做上面这道题时,我强迫症发作,所以每个成员变量后面都加了后缀。后了一想,这样写项目很有用,写算法就是反而很鸡肋。于是我决定,以后写算法不在成员变量后面加后缀下划线了。

看130题。

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X
Subscribe to see which companies asked this question.

首先我直接用DFS:

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if(board.empty())
            return ;
        int m = board.size();
        int n = board[0].size();
        for(int i=0; i<m; ++i)
            if(board[i][0] == 'O') dfs_mark(board, m, n, i, 0);
        for(int j=1; j<n; ++j)
            if(board[0][j] == 'O') dfs_mark(board, m, n, 0, j);    
        for(int i=1; i<m; ++i)
            if(board[i][n-1] == 'O') dfs_mark(board, m, n, i, n-1);
        for(int j=1; j<n-1; ++j)
            if(board[m-1][j] == 'O') dfs_mark(board, m, n, m-1, j);
        for(int i=1; i<m-1; ++i){
            for(int j=1; j<n-1; ++j)
                if(board[i][j] != 'N') board[i][j] = 'X';
        }
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j)
                if(board[i][j] == 'N') board[i][j] = 'O';
        }
    }
    void dfs_mark(vector<vector<char>>& board, int m, int n, int x, int y){
        if(x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'O') return;
        board[x][y] = 'N';
        dfs_mark(board, m, n, x-1, y);
        dfs_mark(board, m, n, x+1, y);
        dfs_mark(board, m, n, x, y-1);
        dfs_mark(board, m, n, x, y+1);
    }
};

然后超时了。但是我在网上搜这道题,在别的OJ上用这份代码通过了。所以leetcode要求还是蛮严格的。

然后是并查集版本:

class Solution {
public:
    vector<vector<char>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    void solve(vector<vector<char>>& board) {
        if(board.empty())
            return;
        int m = board.size();
        int n = board[0].size();
        uf_set us(m*n+1);
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if((i == 0 || i == m-1 || j == 0 || j == n-1) && board[i][j] == 'O')
                    us.union_set(i*n+j, m*n);
                else if(board[i][j] == 'O'){
                    for(auto d : direction){
                        int x = i + d[0];
                        int y = j + d[1];
                        if(board[x][y] == 'O')
                            us.union_set(i*n+j, x*n+y);
                    }
                }
            }
        }

        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                if(us.find_set(i*n+j) != us.find_set(n*m))
                    board[i][j] = 'X';
            }
        }
    }
private:
    class uf_set {
    public:
        uf_set(int sz) : father(new int[sz]), rank(new int[sz]) {
            for(int i=0; i<sz; ++i){
                father[i] = i;
                rank[i] = 0;
            }
        }
        int find_set(int x){
            int root = x;
            while(root != father[root])
                root = father[root];

            while(x != father[x]){
                int y = father[x];
                father[x] = root;
                x = y;
            }
            return root;
        }
        void union_set(int x, int y){
            x = find_set(x), y = find_set(y);
            if(x == y) return;
            if(rank[x] > rank[y]) 
                father[y] = x;
            else 
                father[x] = y, rank[y] += rank[x] == rank[y];
        }
    private:
        int* father;
        int* rank;
    };
};

秒杀之。

最全中文停用词表 本文将github上包括哈工大停用词、四川大学机器智能实验室停用词、百度停用词、中文停用https://github.com/goto456/stopwords 以及最全中文停用词表(1893)https://blog.csdn.net/shijiebei2009/article/details/39696571进行整合得到新的中文停用词表,总共2462个 为止 纵然 all 例如 [④e... 阅读详情

相关推荐

Ubuntu22.04中通过conda快速部署PyTorch开发环境(CPU/GPU双版本实战)

本文详细介绍了在Ubuntu22.04系统中,如何利用Conda高效部署PyTorch开发环境。文章分别提供了CPU与GPU双版本的实战教程,涵盖环境创建、镜像加速、版本匹配及深度验证等关键步骤,旨在帮助开发者快速搭建稳定、兼容的深度学习环境,从而专注于模型开发。

rose2的博客 566

200. Number of Islands (并查集)

200. Number of Islands Description 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 horizon...

Elo的博客 355

Shopee接口安全机制深度解析:从签名参数到稳定采集实战

本文深度解析了Shopee平台接口的安全机制,重点剖析了其核心签名参数`x-sap-ri`和`x-sap-sec`的动态生成逻辑。通过逆向分析与实战演示,文章提供了使用Python模拟签名算法、构建稳定数据采集方案的完整思路与代码框架,并探讨了应对密钥动态更新等长期运行挑战的策略。

weixin_29323049的博客 405

LeetCode305. Number of Islands II - 彻底掌握并查集(Union Find)系列题5

​这道题是Leetcode200. Number of Islands的改进版,很显然可以用并查集(Union Find)来解答。唯一不同的就是陆地是随着positions数组动态增加的,每增加一块陆地就要统计当前岛屿的个数。由于我们再Union Find数据结构里维护了一个岛屿计数的变量,因此每增加一块新陆地,进行更新合并之后,我们就可以立刻得到岛屿的个数。

hgq522的博客 482

GEE——土地利用分类之监督分类

利用GEE做监督分类,以武汉市为例。 训练器我们选择随机森林;数据选择Landsat-8;训练样本使用的是GEE提供的一种,它把地表分为三类:水体、植被和城市用地。 选择训练样本 用要素集来标记训练样本。每个要素都有一个landcover 的属性,0表示城市用地,1表示植被,2表示水体。 对影像进行分类 其中一部分进行分类器的训练,还有一部分留下来做精度验证。 输出影像及混淆矩阵 print(LABEL_DATA); var wuhan = ee.FeatureCollection('users/

qq_41600680的博客 1万+

告别理论劝退:用动画和Tanner图轻松理解QC-LDPC的译码原理(BP算法详解)

本文通过动画和Tanner图直观解析QC-LDPC码的BP译码原理,帮助读者轻松理解802.11ac协议中的编译码过程。文章详细展示了QC-LDPC的结构之美、BP算法的动态信息流动以及LLR的视觉化解读,并对比了硬判决算法的性能差异,为开发者和研究者提供了实用的可视化工具开发建议。

weixin_30411239的博客 498

Leetcode200. Number of Islands - 彻底掌握并查集(Union Find)系列题1

这道的常规解法是用深度优先搜索(DFS)或宽度优先搜索(BFS)来解决。 其实这道题还可以用并查集(Union Find)算法来解决。我们知道并查集(Union Find)是用来解决连通问题,即处理集合的查找合并问题。而这道题里的岛的概念就是把连通的陆地合并成一个岛,相当于是矩阵里所有相连的‘1’合并成一个集合,最后集合个数就是所要求的岛的个数。

hgq522的博客 1082

NVIDIA DGX Spark (Ubuntu 24.04) 部署Isaac Sim与Isaac Lab:二进制安装与ROS2集成实战

本文详细介绍了在NVIDIA DGX Spark工作站(Ubuntu 24.04系统)上,通过预编译二进制包稳定部署Isaac Sim与Isaac Lab的完整实战流程。文章重点剖析了二进制安装相较于源码编译和pip安装的优势,有效规避了CUDA、PyTorch等关键组件的版本冲突陷阱,并逐步指导完成环境配置、Isaac Lab集成、Conda环境管理以及ROS2 Jazzy的集成,最终构建出完整的机器人仿真与强化学习开发平台。

kite3的博客 408

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 assu

无名山丘,崛起成峰 2706

(王道考研计算机网络)第三章数据链路层-第一节:数据链路层功能概述

文章目录一:本章概述二:数据链路层基本概念三:数据链路层功能概述(1)为网络层提供服务(2)链路管理(3)组帧(4)流量控制(5)差错控制 本章是计算机网络中入门较难的一章,如果没有接触过相关内容,可能会止步于一些晦涩难懂的术语。本节内容希望大家可以用心看一看,对于大家入门是很有帮助的 一:本章概述 数据链路层位于物理层之上,主要作用就是提高物理层传输比特流的能力,使物理层可能出现差错的连接转变为逻辑上无差错的数据链路,同时向网络层提供服务。本章学习的主要就是数据链路层的功能和应用 数据链路层功能 封

快乐江湖的博客 2481

UniApp 覆盖安装后首次进入 APP pages.json 不生效的解决方案

通过以上步骤,解决了缓存问题导致的首次加载异常。此方案的关键在于强制更新文件名,从根本上避免旧缓存的干扰。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 701

leetcode 200. Number of Islands-岛屿数量|并查集算法

原题链接:200. Number of Islands 【思路】 本题考查dfs和bfs的结合。由于题目没有限制不可改变原数组的结构,所以一旦遇到1,就将周边相邻的几个1置为2。遍历整个数组,遇到1的个数要求的解: public int numIslands(char[][] grid) { int islansNum = 0; for (int i

hdtopku 3258

C++ 小游戏程序 (共七款)

C++编程小游戏_闪耀的天狼星的博客-CSDN博客_游戏c++C++跑酷小游戏_闪耀的天狼星的博客-CSDN博客。好玩的c++编程小游戏,共六款。代码】C++跑酷小游戏。

小天狼星_布莱克 的博客 3万+

LeetCode(130 & 200):被围绕的区域 & 岛屿数量 Surrounded Regions & Number of Islands(Java)

2019.8.11 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 这两道题特别相似,所以就放到一起讨论了。 第一题是很明显是一个DFS,但是可以优化的地方在于,可以只沿边界的‘O’进行DFS单独标记。最后被标记的‘O’保留,其余未被标记的‘O’改为‘X’。 第二题同样可以用DFS一气呵成,不过还可以尝试使用BFS或者并查集(尽管并查集在本题上并不是个很好的方法)的做法来...

NJU_ChopinXBP的博客 339

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

<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 (land) and '0's (water), count the number of islands. An island is surrounded by water and

EbowTang的练习场 1632

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

总结几道DFS的问题

为了感谢某人花了两天给我讲明白了DFS的问题… 我得赶紧把它们记下来… 问题来自leetcode,问题的集锦来自https://zhuanlan.zhihu.com/p/62884431 #695 这道题给了一个二维数组,0表示水,1表示陆地。问连在一起的1最多有几个。 a 是岛屿的数组 s 是状态数组 i,j代表位置 def dfs(a, s, i, j, ans)://在dfs中,我们首先要修...

qq_34471804的博客 523

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

leetcode思路总结反思(dfs篇)

leetcode思路总结反思(dfs篇) 130. Surrounded Regions (dfs蚕食法专题) 使用dfs,利用“蚕食法”,核心代码模板如下: for(int i=0;i<row;i++) { check(board,i,0,row,col); check(board,i,col-1,row,col); } ...

weixin_43886143的博客 280

leetcode|graph| 399. Evaluate Division 200. Number of Islands 130. Surrounded Regions

在boundary上或者和在boundary上格子相连的格子都不需要变成x。所以只需要思路同上地标记处所有不需要变的0就行(标记#之类的)。剩下的0就是要变成x的。dfs+memoization,需要用一个set记录某个点有没有作为起点使用过。用self,这样numIslands也可以access用dfs。所以这里grid[i][j]的改变会被带到外面。

amytheace的博客 324

LeetCode 200. Number of Islands&130. Surrounded Regions--DFS

两道题应该分别是我在蓝桥杯和南大夏令营遇到的原题,比较相似 题目链接 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 connect...

monkey_rose的博客 257

200. Number of Islands && 130. Surrounded Regions

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

weixin_30535043的博客 86

[LeetCode] BFS解决的题目

一.130 Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题的意思是将所有被X包围的O都变为X(边缘的不算),我们可以维护一个队列,先把四周的O的位置放进队列中,并把这个位置的值变为Y。然后每次从队列中拿出一个位置,把这个位置四周为O的位置...

aixian8156的博客 302

1.5 灵神题单总结_网格图_dfs

岛屿类题目全部基于。

凡物加倍磨治,皆能变化气质 206

LeetCode 130 Surrounded Regions(并查集)

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X

nudt_oys的博客 1146

(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 surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may ass

SuperNova 3828

(LeetCode 130)Surrounded Regions(并查集)

Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.For example, X X X X X O O X X X O X

SuperNova 4028

LeetCode 305. Number of Islands II(小岛)

原题网址:https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at posit

James Pan 3743
上一篇: 235.236:Lowest Common Ancestor of a Binary Search Tree/Tree(LCA问题)
下一篇: epoll内核实现分析
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址