Golang:
思路:简单的并查集问题
代码如下:
func findCircleNum(M [][]int) int {
res:=len(M)
students:=make([]int,len(M))
for k,_ :=range students{
students[k]=k
}
for i:=0;i<len(M);i++{
for j:=i;j<len(M);j++{
if M[i][j]==1 {
res-=UnionFriends(students,i,j)
}
}
}
return res
}
func UnionFriends(stu []int,i,j int) int {
a,b:=findUnion(stu,i),findUnion(stu,j)
if a!=b{
stu[b]=a
return 1
}
return 0
}
func findUnion(stu []int,i int) int{
for stu[i]!=i {
i=stu[i]
}
return i
}
有疑问加站长微信联系(非本文作者)