mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user