Skip to content

Commit d2172e9

Browse files
committed
add leetcode547
1 parent 3b0555e commit d2172e9

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Solution
2+
3+
var p []int
4+
var r []int
5+
var ans int
6+
7+
func findCircleNum(M [][]int) int {
8+
ans = len(M)
9+
initialize(ans)
10+
11+
for i := 0; i < len(M); i++ {
12+
for j := 0; j < i; j++ {
13+
if 1 == M[i][j] {
14+
union(i, j)
15+
}
16+
}
17+
}
18+
return ans
19+
}
20+
21+
func initialize(l int) {
22+
p = make([]int, l)
23+
r = make([]int, l)
24+
for i, _ := range p {
25+
p[i] = i
26+
r[i] = 1
27+
}
28+
}
29+
30+
func find(x int) int {
31+
if x != p[x] {
32+
p[x] = find(p[x])
33+
}
34+
return p[x]
35+
}
36+
37+
func union(x, y int) {
38+
x, y = find(x), find(y)
39+
if x != y {
40+
if r[x] <= r[y] {
41+
p[x] = y
42+
r[y] += r[x]
43+
} else {
44+
p[y] = x
45+
r[x] += r[y]
46+
}
47+
ans--
48+
}
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSolution(t *testing.T) {
9+
// 测试用例
10+
cases := []struct {
11+
name string
12+
inputs[][]int
13+
expect int
14+
}{
15+
{"TestCases 1",
16+
[][]int{
17+
{1,1,0},
18+
{1,1,1},
19+
{0,1,1},
20+
},
21+
1,
22+
},
23+
}
24+
25+
// 开始测试
26+
for _, c := range cases {
27+
t.Run(c.name, func(t *testing.T) {
28+
got := findCircleNum(c.inputs)
29+
if !reflect.DeepEqual(got, c.expect) {
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
31+
}
32+
})
33+
}
34+
}
35+
36+
// 压力测试
37+
func BenchmarkSolution(b *testing.B) {
38+
39+
}
40+
41+
// 使用案列
42+
func ExampleSolution() {
43+
44+
}

src/0547.Friend-Circles/readme.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [547. Friend Circles][title]
2+
3+
## Description
4+
5+
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.
6+
7+
Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.
8+
9+
**Example 1:**
10+
```
11+
Input:
12+
[[1,1,0],
13+
[1,1,0],
14+
[0,0,1]]
15+
Output: 2
16+
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
17+
The 2nd student himself is in a friend circle. So return 2.
18+
```
19+
20+
**Example 2:**
21+
```
22+
Input:
23+
[[1,1,0],
24+
[1,1,1],
25+
[0,1,1]]
26+
Output: 1
27+
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
28+
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
29+
```
30+
31+
## 题意
32+
> 判断几个学生是否在同一个圈子。
33+
34+
## 题解
35+
36+
### 思路 1
37+
> 使用并查集
38+
39+
```go
40+
var p []int
41+
var r []int
42+
var ans int
43+
44+
func findCircleNum(M [][]int) int {
45+
ans = len(M)
46+
initialize(ans)
47+
48+
for i := 0; i < len(M); i++ {
49+
for j := 0; j < i; j++ {
50+
if 1 == M[i][j] {
51+
union(i, j)
52+
}
53+
}
54+
}
55+
return ans
56+
}
57+
58+
func initialize(l int) {
59+
p = make([]int, l)
60+
r = make([]int, l)
61+
for i, _ := range p {
62+
p[i] = i
63+
r[i] = 1
64+
}
65+
fmt.Println(p, r)
66+
}
67+
68+
func find(x int) int {
69+
if x != p[x] {
70+
p[x] = find(p[x])
71+
}
72+
return p[x]
73+
}
74+
75+
func union(x, y int) {
76+
x, y = find(x), find(y)
77+
if x != y {
78+
if r[x] <= r[y] {
79+
p[x] = y
80+
r[y] += r[x]
81+
} else {
82+
p[y] = x
83+
r[x] += r[y]
84+
}
85+
ans--
86+
}
87+
}
88+
```
89+
90+
## 结语
91+
92+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
93+
94+
[title]: https://leetcode.com/problems/friend-circles/description/
95+
[me]: https://github.com/kylesliu/awesome-golang-leetcode

0 commit comments

Comments
 (0)