当前位置:网站首页>Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
2022-04-23 18:53:00 【Mr Gao】
The finger of the sword Offer II 116. Number of provinces
Yes n Cities , Some of them are connected to each other , Others are not connected . If the city a And the city b Direct connection , And the city b And the city c Direct connection , So the city a And the city c Indirectly connected .
Province It's a group of directly or indirectly connected cities , There are no other cities in the group that are not connected .
To give you one n x n Matrix isConnected , among isConnected[i][j] = 1 It means the first one i Two cities and j The two cities are directly connected , and isConnected[i][j] = 0 The two are not directly connected .
Back in the matrix Province The number of .
Example 1:
Input :isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output :2
Example 2:
Input :isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output :3
As follows: , Simple recursive method
void f2(int** isConnected, int n,int *r,int j){
int i;
r[j]=0;
for(i=0;i<n;i++){
if(isConnected[j][i]==1&&r[i]==1&&i!=j){
r[i]=0;
f2(isConnected,n,r,i);
}
}
}
void f(int** isConnected, int n,int *r,int *sum){
int i,j;
for(i=0;i<n;i++){
if(r[i]==1){
r[i]=0;
(*sum)++;
for(j=0;j<n;j++){
if(isConnected[i][j]==1&&r[j]==1&&i!=j){
f2(isConnected,n,r,j);
}
}
}
}
}
int findCircleNum(int** isConnected, int isConnectedSize, int* isConnectedColSize){
int n=isConnectedSize;
int *sum=(int *)malloc(sizeof(int));
int r[isConnectedSize];
int i;
for(i=0;i<n;i++){
r[i]=1;
}
*sum=0;
f(isConnected,n,r,sum);
return *sum;
}
版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231851498735.html
边栏推荐
- Seata handles distributed transactions
- Recyclerview control list item layout match_ Fundamental principle of parent attribute invalidation
- 七、DOM(下) - 章节课后练习题及答案
- 电路在线模拟
- 视频边框背景如何虚化,简单操作几步实现
- Query the logistics update quantity according to the express order number
- PyGame tank battle
- Tencent map and high logo removal method
- Minesweeping II of souI instance
- 22 year flying Book manpower Kit
猜你喜欢
【C语言进阶11——字符和字符串函数及其模拟实现(2))】
【科普】CRC校验(一)什么是CRC校验?
iptables初探
mysql_ Download and installation of Linux version
c#:泛型反射
ESP32 LVGL8. 1 - label (style 14)
mysql_linux版本的下載及安裝詳解
mysql_linux版本的下载及安装详解
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
随机推荐
使用晨曦记账本,分析某个时间段每个账户收支结余
ESP32 LVGL8. 1 - arc (arc 19)
Nacos集群搭建和mysql持久化配置
K210 serial communication
7、 DOM (Part 2) - chapter after class exercises and answers
Accessing private members using templates
Use of content provider
Nacos作为服务配置中心实战
Implementation of TCP UDP communication with golang language
mysql_ Download and installation of Linux version
Sentinel rule persistence into Nacos
ESP32 LVGL8. 1 - slider slider (slider 22)
Raspberry pie uses root operation, and the graphical interface uses its own file manager
Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
Simple use of navigation in jetpack
中金财富怎么样?在上边开户安全吗
回路-通路
: app: transformclasseswithrobustfordevrease meituan hot repair compilation error record
ctfshow-web361(SSTI)
机器学习理论之(8):模型集成 Ensemble Learning