We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6adf2e3 commit 3c7bb86Copy full SHA for 3c7bb86
DFS/traditionalDFS/547.md
@@ -0,0 +1,41 @@
1
+## 547 Friend Circles
2
+
3
+#### Description
4
5
+[link](https://leetcode.com/problems/friend-circles/)
6
7
+---
8
9
+#### Solution
10
11
+- 并查集解法 union find,把所有见到过的全部放到记录set当中,保证主循环中没有见到过的都是新的集合
12
13
14
15
+#### Code
16
17
+> 最坏情况:O(n^2)
18
19
+```python
20
+class Solution:
21
+ def findCircleNum(self, M: List[List[int]]) -> int:
22
+ if not M:
23
+ return 0
24
25
+ visited = set()
26
+ N = len(M)
27
+ res = 0
28
+ for i in range(N):
29
+ if i not in visited:
30
+ res += 1
31
+ self.dfs(M, i, visited)
32
+ return res
33
34
35
+ def dfs(self, M, i, visited):
36
+ visited.add(i)
37
+ for idx, adj in enumerate(M[i]):
38
+ if adj == 1 and idx not in visited:
39
+ self.dfs(M, idx, visited)
40
+ return
41
+```
0 commit comments