From 23ca9e43e5a15bdbbf69744515b94736ac1d3b0d Mon Sep 17 00:00:00 2001 From: CH Ye Date: Fri, 15 Sep 2023 16:09:38 -0400 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86684.=E5=86=97?= =?UTF-8?q?=E4=BD=99=E8=BF=9E=E6=8E=A5=20=E7=9A=84Python=E5=B9=B6=E6=9F=A5?= =?UTF-8?q?=E9=9B=86=E7=AE=80=E6=B4=81=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0684.冗余连接.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index 8124cc7e..f5e84223 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -256,6 +256,23 @@ class Solution: 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