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

Commit 134692e

Browse files
Merge pull request youngyangyang04#2279 from Snow-Ye/master
添加了684.冗余连接 的Python并查集简洁写法 , 添加了 1971. 寻找图中是否存在路径 的Python并查集解法
2 parents a47de38 + 23ca9e4 commit 134692e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

‎problems/0684.冗余连接.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ class Solution:
256256
return []
257257
```
258258

259+
### Python简洁写法:
260+
261+
```python
262+
class Solution:
263+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
264+
n = len(edges)
265+
p = [i for i in range(n+1)]
266+
def find(i):
267+
if p[i] != i:
268+
p[i] = find(p[i])
269+
return p[i]
270+
for u, v in edges:
271+
if p[find(u)] == find(v):
272+
return [u, v]
273+
p[find(u)] = find(v)
274+
```
275+
259276
### Go
260277

261278
```go

‎problems/1971.寻找图中是否存在路径.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ public:
134134
}
135135
};
136136
```
137+
138+
PYTHON并查集解法如下:
139+
```PYTHON
140+
class Solution:
141+
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
142+
p = [i for i in range(n)]
143+
def find(i):
144+
if p[i] != i:
145+
p[i] = find(p[i])
146+
return p[i]
147+
for u, v in edges:
148+
p[find(u)] = find(v)
149+
return find(source) == find(destination)
150+
```
151+
152+
137153
<p align="center">
138154
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
139155
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /