diff --git "a/problems/0028.345円256円236円347円216円260円strStr.md" "b/problems/0028.345円256円236円347円216円260円strStr.md" index 207a047d60..629ff014dd 100644 --- "a/problems/0028.345円256円236円347円216円260円strStr.md" +++ "b/problems/0028.345円256円236円347円216円260円strStr.md" @@ -207,7 +207,7 @@ next数组就是一个前缀表(prefix table)。 ### 前缀表与next数组 -很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? +很多KMP算法的实现都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? next数组就可以是前缀表,但是很多实现都是把前缀表统一减一(右移一位,初始位置为-1)之后作为next数组。 diff --git "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" index 8124cc7eea..f5e84223b1 100644 --- "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" +++ "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.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 diff --git "a/problems/1971.345円257円273円346円211円276円345円233円276円344円270円255円346円230円257円345円220円246円345円255円230円345円234円250円350円267円257円345円276円204円.md" "b/problems/1971.345円257円273円346円211円276円345円233円276円344円270円255円346円230円257円345円220円246円345円255円230円345円234円250円350円267円257円345円276円204円.md" index 29e50ab8b0..5660233cf6 100644 --- "a/problems/1971.345円257円273円346円211円276円345円233円276円344円270円255円346円230円257円345円220円246円345円255円230円345円234円250円350円267円257円345円276円204円.md" +++ "b/problems/1971.345円257円273円346円211円276円345円233円276円344円270円255円346円230円257円345円220円246円345円255円230円345円234円250円350円267円257円345円276円204円.md" @@ -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) +``` + +