每日一道Leetcode——省份数量
作者:互联网
题目:
我的解法:Folyd算法
class Solution {
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(isConnected[i][k]==1 && isConnected[k][j]==1){
isConnected[i][j] = 1;
isConnected[j][i] = 1;
}
}
}
}
int num = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(0);
for(int i=1; i<n; i++){
boolean b = false;
for(int j=0; j<list.size(); j++){
if(isConnected[i][list.get(j)]==1){
list.add(i);
b = true;
break;
}
}
if(!b){
list.add(i);
num++;
}
}
return num;
}
}
官方题解:
标签:int,题解,每日,list,isConnected,add,num,省份,Leetcode 来源: https://blog.csdn.net/weixin_41945051/article/details/112310822