Skip to content

Commit 0d44a5d

Browse files
committed
优化1209
1 parent 41e6c52 commit 0d44a5d

File tree

6 files changed

+23
-25
lines changed

6 files changed

+23
-25
lines changed

docs/1209-remove-all-adjacent-duplicates-in-string-ii.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string-ii/[LeetCo
4747
4848
== 思路分析
4949

50+
最初的想法是暴力解法:对重复的相邻字母计数,当计数达到 k 时将其删除。重复此操作,直到没有删除的字符为止。通过 19 / 21 个测试用例。
51+
52+
暴力解法最大的问题就是字符次数的重复计算,可以开辟一个数组,记录每个下标对应的出现次数,这样直接在上一个基础上+1或从1开始计数即可。
53+
54+
image::images/1209-10.png[{image_attr}]
55+
56+
image::images/1209-11.png[{image_attr}]
57+
58+
也可以用栈!不过,在没有重复字符的情况下,空间复杂度相比用数组更消耗空间。
59+
60+
image::images/1209-12.gif[{image_attr}]
5061

5162
[[src-1209]]
5263
[tabs]
@@ -73,4 +84,4 @@ include::{sourcedir}/_1209_RemoveAllAdjacentDuplicatesInStringIi.java[tag=answer
7384

7485
== 参考资料
7586

76-
87+
. https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string-ii/solutions/99459/shan-chu-zi-fu-chuan-zhong-de-suo-you-xiang-lin--4/[1209. 删除字符串中的所有相邻重复项 II - 官方题解^] -- 多种解法,值得学习!

docs/images/1209-10.png

9.95 KB
Loading

docs/images/1209-11.png

9.93 KB
Loading

docs/images/1209-12.gif

347 KB
Loading

logbook/202503.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ endif::[]
896896
|{counter:codes2503}
897897
|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string-ii/[1209. 删除字符串中的所有相邻重复项 II^]
898898
|{doc_base_url}/1209-remove-all-adjacent-duplicates-in-string-ii.adoc[题解]
899-
|✅ 暴力解法:对重复的相邻字母计数,当计数达到 k 时将其删除。重复此操作,直到没有删除的字符为止。通过 19 / 21 个测试用例。
899+
|✅ 暴力解法:对重复的相邻字母计数,当计数达到 k 时将其删除。重复此操作,直到没有删除的字符为止。通过 19 / 21 个测试用例。更优解:把当前字符的次数存下来,下一个字符就可以在当前字符基础上做处理,省去重复计算。官方题解提供了多种解法,非常优秀!
900900

901901

902902
|===

src/main/java/com/diguage/algo/leetcode/_1209_RemoveAllAdjacentDuplicatesInStringIi.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,24 @@ public class _1209_RemoveAllAdjacentDuplicatesInStringIi {
44
// tag::answer[]
55

66
/**
7-
* 暴力解法: 通过 19 / 21 个测试用例。
8-
*
97
* @author D瓜哥 · https://www.diguage.com
108
* @since 2025-06-02 07:27:39
119
*/
1210
public String removeDuplicates(String s, int k) {
13-
if (s == null || s.length() < k) {
14-
return s;
15-
}
1611
StringBuilder sb = new StringBuilder(s);
17-
int length = sb.length();
18-
int index = 0;
19-
while (index < length) {
20-
char c = sb.charAt(index);
21-
int fast = index + 1;
22-
while (fast < length && c == sb.charAt(fast) && fast - index < k) {
23-
fast++;
24-
}
25-
int dc = fast - index;
26-
if (fast <= length && dc >= k) {
27-
sb.replace(index, fast, "");
28-
length = sb.length();
12+
int[] count = new int[s.length()];
13+
for (int i = 0; i < sb.length(); i++) {
14+
if (i == 0 || sb.charAt(i) != sb.charAt(i - 1)) {
15+
count[i] = 1;
2916
} else {
30-
index = fast;
17+
count[i] = count[i - 1] + 1;
18+
if (count[i] == k) {
19+
sb.delete(i - k + 1, i + 1);
20+
i -= k;
21+
}
3122
}
3223
}
33-
if (sb.length() == s.length()) {
34-
return s;
35-
} else {
36-
return removeDuplicates(sb.toString(), k);
37-
}
24+
return sb.toString();
3825
}
3926

4027
// end::answer[]

0 commit comments

Comments
 (0)