mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
# 添加了 1971. 寻找图中是否存在路径 的Python并查集解法
This commit is contained in:
@ -134,6 +134,22 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
PYTHON并查集解法如下:
|
||||
```PYTHON
|
||||
class Solution:
|
||||
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
|
||||
p = [i for i in range(n)]
|
||||
def find(i):
|
||||
if p[i] != i:
|
||||
p[i] = find(p[i])
|
||||
return p[i]
|
||||
for u, v in edges:
|
||||
p[find(u)] = find(v)
|
||||
return find(source) == find(destination)
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user