当前位置:网站首页>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
边栏推荐
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- The type initializer for ‘Gdip‘ threw an exception
- listener.log
- Treatment of incomplete display of listview height
- Introduction to ROS learning notes (I)
- 在渤海期货办理开户安全吗。
- Implementation of TCP UDP communication with golang language
- ESP32 LVGL8. 1 - arc (arc 19)
- Simple use of viewbinding
- MVVM模型
猜你喜欢

PyGame tank battle

Introduction to ROS learning notes (II)

On iptables

ESP32 LVGL8. 1 - label (style 14)

ESP32 LVGL8. 1 - arc (arc 19)

Introduction to micro build low code zero Foundation (lesson 3)

Practice of Druid SQL and security in meituan review

简化路径(力扣71)

微搭低代码零基础入门课(第三课)

【C语言进阶11——字符和字符串函数及其模拟实现(2))】
随机推荐
Esp32 (UART ecoh) - serial port echo worm learning (2)
Simple use of viewbinding
Recyclerview control list item layout match_ Fundamental principle of parent attribute invalidation
Introduction to ROS learning notes (I)
Setting up keil environment of GD single chip microcomputer
Minesweeping II of souI instance
Usage of functions decode() and replace() in SQL
[today in history] April 23: the first video uploaded on YouTube; Netease cloud music officially launched; The inventor of digital audio player was born
Simple use of navigation in jetpack
22年字节跳动飞书人力套件三面面经
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Sentinel service fusing practice (sentinel integration ribbon + openfeign + fallback)
数据库上机实验四(数据完整性与存储过程)
Query the logistics update quantity according to the express order number
Sentinel服务熔断实战(sentinel整合ribbon+openFeign+fallback)
特征选择feature_selection--SelectKBest
os_ authent_ Prefix
配置iptables
MySQL Téléchargement et installation de la version Linux
Esp32 (UART 485 communication) - 485 communication of serial port (3)