当前位置:网站首页>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
边栏推荐
- mysql_ Download and installation of Linux version
- mysql_linux版本的下載及安裝詳解
- Go language GUI framework Fyne Chinese garbled or not displayed
- Use of kotlin collaboration in the project
- Druid SQL和Security在美团点评的实践
- Advanced transfer learning
- Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
- ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
- Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
猜你喜欢
教你用简单几个步骤快速重命名文件夹名
机器学习理论之(8):模型集成 Ensemble Learning
12个例子夯实promise基础
ESP32 LVGL8. 1 - img picture (IMG 20)
STM32: LCD display
Introduction to micro build low code zero Foundation (lesson 3)
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
Practice of Druid SQL and security in meituan review
MVVM model
根据快递单号查询物流查询更新量
随机推荐
迁移学习进阶
ESP32 LVGL8. 1 - bar progress bar (bar 21)
ESP32 LVGL8. 1 - calendar (calendar 25)
微搭低代码零基础入门课(第三课)
Domestic GD chip can filter
中金财富怎么样?在上边开户安全吗
Query the logistics update quantity according to the express order number
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Fundamentals of machine learning theory -- some terms about machine learning
Solutions such as unknown or garbled code or certificate problem prompt in Charles's mobile phone packet capture, actual measurement.
Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
MySQL学习第五弹——事务及其操作特性详解
iptables -L执行缓慢
玻璃体中的硫酸软骨素
Tencent map and high logo removal method
程序员如何快速开发高质量的代码?
【C语言进阶11——字符和字符串函数及其模拟实现(2))】
Feature selection feature_ selection--SelectKBest
Use of content provider
Machine learning practice - naive Bayes