当前位置:网站首页>leetcode-547:省份数量
leetcode-547:省份数量
2022-07-30 17:38:00 【菊头蝙蝠】
题目
有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连。
省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。
给你一个 n x n 的矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i 个城市和第 j 个城市直接相连,而 isConnected[i][j] = 0 表示二者不直接相连。
返回矩阵中 省份 的数量。
示例 1:
输入:isConnected = [[1,1,0],[1,1,0],[0,0,1]]
输出:2
示例 2:

输入:isConnected = [[1,0,0],[0,1,0],[0,0,1]]
输出:3
解题
方法一:并查集
class UnionFind{
private:
vector<int> parent;
int setNum;
public:
UnionFind(int n){
parent.resize(n);
iota(parent.begin(),parent.end(),0);
setNum=n;//初始集合数量为n
}
int find(int index){
if(parent[index]==index) return index;
parent[index]=find(parent[index]);
return parent[index];
}
void unite(int index1,int index2){
int p1=find(index1);
int p2=find(index2);
if(p1!=p2){
//如果两个集合不是同一个,那么合并集合,集合数量减一
parent[p1]=p2;
setNum--;
}
}
int getSetNum(){
return setNum;
}
};
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
int n=isConnected.size();
UnionFind uf(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(isConnected[i][j]==1){
uf.unite(i,j);
}
}
}
return uf.getSetNum();
}
};
方法二:dfs
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
int n=isConnected.size();
int res=0;
vector<bool> visit(n);
for(int i=0;i<n;i++){
if(!visit[i]){
dfs(isConnected,visit,i);
res++;
}
}
return res;
}
void dfs(vector<vector<int>>& isConnected,vector<bool>& visit,int i){
visit[i]=true;
for(int j=0;j<visit.size();j++){
if(!visit[j]&&isConnected[i][j]){
dfs(isConnected,visit,j);
}
}
}
};
边栏推荐
- 一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
- (17)[系统调用]追踪系统调用(0环)
- Insert data into MySQL in C language
- 莫队--优雅的暴力
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 想要写出好的测试用例,先要学会测试设计
- Google earth engine如何实现我们时间列表的排列和选取
- 数据库系统原理与应用教程(068)—— MySQL 练习题:操作题 90-94(十二):DML 语句练习
- 什么是工业射线照相设备?
- How Google earth engine realizes the arrangement and selection of our time list
猜你喜欢

esp32系列(5):esp32 蓝牙架构学习

C语言向MySQL插入数据

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions

基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇

查询表中开始日期与结束日期

知识蒸馏2:目标检测中的知识蒸馏

LeetCode318: Maximum product of word lengths

Tensorflow中实现正则化

C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
随机推荐
Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
mysql刷脏的几种场景以及相关参数
C陷阱与缺陷 第6章 预处理器
FastJson反序列化漏洞(复现)
FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
Google earth engine如何实现我们时间列表的排列和选取
信息学奥赛一本通 1966:【14NOIP普及组】比例简化 | 洛谷 P2118 [NOIP2014 普及组] 比例简化
如何让 JOIN 跑得更快?
将 APACHE 日志解析到 SQL 数据库中
SLIM: Sparse Linear Methods (TopN推荐)
KDD 2020 | 深入浅出优势特征蒸馏在淘宝推荐中的应用
Web 3.0入门教程
顺通海关查验预约综合管理系统
知识蒸馏1:基础原理讲解及yolov5项目实战介绍
Microsoft Office 2019 software download and installation detailed tutorial!
Excel导入和导出
Valid bracketed strings [greedy exercise]
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
数据库系统原理与应用教程(063)—— MySQL 练习题:操作题 39-50(七):SELECT 基本语法联系