mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加了684.冗余连接 的Python并查集简洁写法
This commit is contained in:
@ -256,6 +256,23 @@ class Solution:
|
|||||||
return []
|
return []
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Python简洁写法:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
|
||||||
|
n = len(edges)
|
||||||
|
p = [i for i in range(n+1)]
|
||||||
|
def find(i):
|
||||||
|
if p[i] != i:
|
||||||
|
p[i] = find(p[i])
|
||||||
|
return p[i]
|
||||||
|
for u, v in edges:
|
||||||
|
if p[find(u)] == find(v):
|
||||||
|
return [u, v]
|
||||||
|
p[find(u)] = find(v)
|
||||||
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user