本题力扣传送门
这道题开始新的知识点,并查集,看了助教的写法,然后去听了船长的基本知识以后,就先跟着助教的代码写一个模版.代码如下
/*
* @lc app=leetcode.cn id=547 lang=javascript
*
* [547] 省份数量
*/
// @lc code=start
/**
* @param {number[][]} isConnected
* @return {number}
*/
var findCircleNum = function (isConnected) {
let circleNum = isConnected.length;
let uf = new UnitFind(circleNum);
for (let i = 0; i < circleNum; i++) {
for (let j = i + 1; j < circleNum; j++) {
// 只有相连 才能合并
if (isConnected[i][j] === 1) {
uf.unite(i, j)
}
}
}
// 合并以后的数量
return uf.getCount()
};
class UnitFind {
constructor(n) {
this.parent = new Array(n).fill(0).map((value, index) => index);
this.rank = new Array(n).fill(1);
this.setCount = n;
}
findSet(index) {
if (this.parent[index] != index) {
this.parent[index] = this.findSet(this.parent[index])
}
return this.parent[index]
}
unite(index1, index2) {
let root1 = this.findSet(index1), root2 = this.findSet(index2);
if (root1 !== root2) {
// 这一步不离基
// 就是把节点少的往节点多的合并
if (root1 < root2) {
[root1, root2] = [root2, root1]
}
// 合并 这一步明白
this.parent[root2] = root1;
// 计算合并的疏朗
this.rank[root1] += this.rank[root2];
//合并以后减少
this.setCount--;
}
}
getCount() {
return this.setCount;
}
// 判断两个节点是否联通 就是看他们的根节点是否相同
connected(index1, index2) {
let root1 = this.findSet(index1), root2 = this.findSet(index2);
return root1 === root2;
}
}
// @lc code=end