diff --git a/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md b/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md index 2800d2e412879..3014e57384fa9 100644 --- a/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md +++ b/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md @@ -39,7 +39,7 @@ tags:
输入:s = "deeedbbcccbdaa", k = 3 输出:"aa" -解释: +解释: 先删除 "eee" 和 "ccc",得到 "ddbbbdaa" 再删除 "bbb",得到 "dddaa" 最后删除 "ddd",得到 "aa"@@ -81,22 +81,15 @@ tags: ```python class Solution: def removeDuplicates(self, s: str, k: int) -> str: - t = [] - i, n = 0, len(s) - while i < n: - j = i - while j < n and s[j] == s[i]: - j += 1 - cnt = j - i - cnt %= k - if t and t[-1][0] == s[i]: - t[-1][1] = (t[-1][1] + cnt) % k - if t[-1][1] == 0: - t.pop() - elif cnt: - t.append([s[i], cnt]) - i = j - ans = [c * v for c, v in t] + stk = [] + for c in s: + if stk and stk[-1][0] == c: + stk[-1][1] = (stk[-1][1] + 1) % k + if stk[-1][1] == 0: + stk.pop() + else: + stk.append([c, 1]) + ans = [c * v for c, v in stk] return "".join(ans) ``` @@ -190,31 +183,4 @@ type pair struct { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def removeDuplicates(self, s: str, k: int) -> str: - stk = [] - for c in s: - if stk and stk[-1][0] == c: - stk[-1][1] = (stk[-1][1] + 1) % k - if stk[-1][1] == 0: - stk.pop() - else: - stk.append([c, 1]) - ans = [c * v for c, v in stk] - return "".join(ans) -``` - - - - - diff --git a/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README_EN.md b/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README_EN.md index bda61eb710715..4340db28cc1ea 100644 --- a/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README_EN.md +++ b/solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README_EN.md @@ -38,7 +38,7 @@ tags:
Input: s = "deeedbbcccbdaa", k = 3 Output: "aa" -Explanation: +Explanation: First delete "eee" and "ccc", get "ddbbbdaa" Then delete "bbb", get "dddaa" Finally delete "ddd", get "aa"@@ -80,22 +80,15 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is ```python class Solution: def removeDuplicates(self, s: str, k: int) -> str: - t = [] - i, n = 0, len(s) - while i < n: - j = i - while j < n and s[j] == s[i]: - j += 1 - cnt = j - i - cnt %= k - if t and t[-1][0] == s[i]: - t[-1][1] = (t[-1][1] + cnt) % k - if t[-1][1] == 0: - t.pop() - elif cnt: - t.append([s[i], cnt]) - i = j - ans = [c * v for c, v in t] + stk = [] + for c in s: + if stk and stk[-1][0] == c: + stk[-1][1] = (stk[-1][1] + 1) % k + if stk[-1][1] == 0: + stk.pop() + else: + stk.append([c, 1]) + ans = [c * v for c, v in stk] return "".join(ans) ``` @@ -189,31 +182,4 @@ type pair struct { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def removeDuplicates(self, s: str, k: int) -> str: - stk = [] - for c in s: - if stk and stk[-1][0] == c: - stk[-1][1] = (stk[-1][1] + 1) % k - if stk[-1][1] == 0: - stk.pop() - else: - stk.append([c, 1]) - ans = [c * v for c, v in stk] - return "".join(ans) -``` - - - - - diff --git a/solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md b/solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md index a2772d267dc38..6c5ac877e0fee 100644 --- a/solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md +++ b/solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md @@ -83,7 +83,7 @@ tags: 将所有偶数下标的芯片移动到 0 号位置,所有奇数下标的芯片移动到 1 号位置,所有的代价为 0,接下来只需要在 0/1 号位置中选择其中一个较小数量的芯片,移动到另一个位置。所需的最小代价就是那个较小的数量。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为芯片的数量。 +时间复杂度 $O(n)$,其中 $n$ 为芯片的数量。空间复杂度 $O(1)$。 diff --git a/solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README.md b/solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README.md index fd82a5c13f2c5..9069868c73db7 100644 --- a/solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README.md +++ b/solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README.md @@ -66,7 +66,13 @@ tags: ### 方法一:动态规划 -时间复杂度 $O(n)$。 +我们可以使用哈希表 $f$ 来存储以 $x$ 结尾的最长等差子序列的长度。 + +遍历数组 $\textit{arr}$,对于每个元素 $x$,我们更新 $f[x]$ 为 $f[x - \textit{difference}] + 1$。 + +遍历结束后,我们返回 $f$ 中的最大值作为答案返回即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。 @@ -127,6 +133,25 @@ func longestSubsequence(arr []int, difference int) (ans int) { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn longest_subsequence(arr: Vec