Leetcode—547.省份数量【中等】
2023每日刷题(八)
Leetcode—547.省份数量
实现代码
static int father[210] = {0};int Find(int x) {if(x != father[x]) {father[x] = Find(father[x]);}return father[x];
}void Union(int x, int y) {int a = Find(x);int b = Find(y);if(a != b) {father[a] = b;}
}int findCircleNum(int** isConnected, int isConnectedSize, int* isConnectedColSize){int i;for(i = 0; i < isConnectedSize; i++) {father[i] = i;}int j, k = 1;*isConnectedColSize = isConnectedSize;for(i = 1; i < isConnectedSize; i++) {for(j = 0; j < i; j++) {if(isConnected[i][j]) {Union(i, j);}}}int ans = 0;for(i = 0; i < isConnectedSize; i++) {if(i == Find(i)) {ans++;}}return ans;
}
测试结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!