Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit07eb507

Browse files
authored
Update union_find.md
1 parent56ee84d commit07eb507

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

‎data_structure/union_find.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ class Solution:
3535
return edge
3636
```
3737

38+
```Python
39+
classSolution:
40+
deffindRedundantConnection(self,edges: List[List[int]]) -> List[int]:
41+
parents=list(range(len(edges)+1))
42+
43+
deffind(index):
44+
if parents[index]!= index:
45+
parents[index]= find(parents[index])
46+
return parents[index]
47+
48+
defunion(index1,index2):
49+
parents[find(index1)]= find(index2)
50+
51+
for node1, node2in edges:
52+
if find(node1)!= find(node2):
53+
union(node1, node2)
54+
else:
55+
return [node1, node2]
56+
return []
57+
```
58+
3859
###[accounts-merge](https://leetcode-cn.com/problems/accounts-merge/)
3960

4061
```Python
@@ -163,4 +184,4 @@ class Solution:
163184
for w, x, yin edges:
164185
if union(x, y)and find(0)== find(N):# early return without constructing MST
165186
return w
166-
```
187+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp