当前位置:网站首页>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
边栏推荐
- ESP32 LVGL8. 1 - checkbox (checkbox 23)
- 解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
- Tencent map and high logo removal method
- Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
- How can programmers quickly develop high-quality code?
- Nacos集群搭建和mysql持久化配置
- 12 examples to consolidate promise Foundation
- Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
- Machine learning practice - naive Bayes
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
猜你喜欢

ctfshow-web361(SSTI)

ESP32 LVGL8. 1 - img picture (IMG 20)

ESP32 LVGL8. 1 - input devices (input devices 18)

使用晨曦记账本,分析某个时间段每个账户收支结余

Machine learning theory (8): model integration ensemble learning

listener.log

mysql_linux版本的下载及安装详解

解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本

Esp32 (UART event) - serial port event learning (1)

The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
随机推荐
ESP32 LVGL8. 1 - checkbox (checkbox 23)
Iptables - L executes slowly
ESP32 LVGL8. 1 - arc (arc 19)
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
os_authent_prefix
Machine learning theory (8): model integration ensemble learning
迁移学习进阶
Nacos作为服务配置中心实战
ctfshow-web361(SSTI)
回路-通路
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
根据快递单号查询物流查询更新量
Yyds dry goods inventory stringprep --- Internet string preparation
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
Machine learning practice - naive Bayes
Disable Ctrl + Alt + Del
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
ctfshow-web361(SSTI)
ESP32 LVGL8. 1 - calendar (calendar 25)