Skip to content

Commit 9f54ee1

Browse files
committed
一刷1047
1 parent 7102102 commit 9f54ee1

File tree

5 files changed

+94
-29
lines changed

5 files changed

+94
-29
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7354,14 +7354,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
73547354
//|{doc_base_url}/1046-last-stone-weight.adoc[题解]
73557355
//|Easy
73567356
//|
7357-
//
7358-
//|{counter:codes}
7359-
//|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string/[1047. Remove All Adjacent Duplicates In String^]
7360-
//|{source_base_url}/_1047_RemoveAllAdjacentDuplicatesInString.java[Java]
7361-
//|{doc_base_url}/1047-remove-all-adjacent-duplicates-in-string.adoc[题解]
7362-
//|Easy
7363-
//|
7364-
//
7357+
7358+
|{counter:codes}
7359+
|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string/[1047. Remove All Adjacent Duplicates In String^]
7360+
|{source_base_url}/_1047_RemoveAllAdjacentDuplicatesInString.java[Java]
7361+
|{doc_base_url}/1047-remove-all-adjacent-duplicates-in-string.adoc[题解]
7362+
|Easy
7363+
|
7364+
73657365
//|{counter:codes}
73667366
//|{leetcode_base_url}/longest-string-chain/[1048. Longest String Chain^]
73677367
//|{source_base_url}/_1048_LongestStringChain.java[Java]
Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
11
[#1047-remove-all-adjacent-duplicates-in-string]
2-
= 1047. Remove All Adjacent Duplicates In String
2+
= 1047. 删除字符串中的所有相邻重复项
33

4-
{leetcode}/problems/remove-all-adjacent-duplicates-in-string/[LeetCode - Remove All Adjacent Duplicates In String^]
4+
https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/[LeetCode - 1047. 删除字符串中的所有相邻重复项 ^]
55

6-
Given a string `S` of lowercase letters, a _duplicate removal_ consists of choosing two adjacent and equal letters, and removing them.
6+
给出由小写字母组成的字符串 `s`**重复项删除操作**会选择两个相邻且相同的字母,并删除它们。
77

8-
We repeatedly make duplicate removals on S until we no longer can.
8+
`s` 上反复执行重复项删除操作,直到无法继续删除。
99

10-
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
10+
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
1111

12-
12+
*示例:*
1313

14-
*Example 1:*
14+
....
15+
输入:"abbaca"
16+
输出:"ca"
17+
解释:
18+
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
19+
....
1520

16-
[subs="verbatim,quotes,macros"]
17-
----
18-
*Input:* "abbaca"
19-
*Output:* "ca"
20-
*Explanation:*
21-
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
22-
----
23-
24-
25-
26-
*Note:*
21+
*提示:*
2722

23+
. `1 \<= s.length \<= 10^5^`
24+
. `s` 仅由小写英文字母组成。
2825

29-
. `1 <= S.length <= 20000`
30-
. `S` consists only of English lowercase letters.
3126

27+
== 思路分析
3228

29+
把 `StringBuilder` 当做栈来使用。将最后一个字符与当前字符比较,相等则删除最后一个字符,否则就添加。
3330

3431
[[src-1047]]
32+
[tabs]
33+
====
34+
一刷::
35+
+
36+
--
3537
[{java_src_attr}]
3638
----
3739
include::{sourcedir}/_1047_RemoveAllAdjacentDuplicatesInString.java[tag=answer]
3840
----
41+
--
42+
43+
// 二刷::
44+
// +
45+
// --
46+
// [{java_src_attr}]
47+
// ----
48+
// include::{sourcedir}/_1047_RemoveAllAdjacentDuplicatesInString_2.java[tag=answer]
49+
// ----
50+
// --
51+
====
52+
53+
54+
== 参考资料
3955

56+
. https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/solutions/643955/shan-chu-zi-fu-chuan-zhong-de-suo-you-xi-4ohr/[1047. 删除字符串中的所有相邻重复项 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ include::1041-robot-bounded-in-circle.adoc[leveloffset=+1]
21762176

21772177
// include::1046-last-stone-weight.adoc[leveloffset=+1]
21782178

2179-
// include::1047-remove-all-adjacent-duplicates-in-string.adoc[leveloffset=+1]
2179+
include::1047-remove-all-adjacent-duplicates-in-string.adoc[leveloffset=+1]
21802180

21812181
// include::1048-longest-string-chain.adoc[leveloffset=+1]
21822182

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ endif::[]
473473
|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
474474
|❌ 单调栈。一脸懵逼。哨兵技巧非常巧妙。
475475

476+
|{counter:codes2503}
477+
|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string/[1047. 删除字符串中的所有相邻重复项^]
478+
|{doc_base_url}/1047-remove-all-adjacent-duplicates-in-string.adoc[题解]
479+
|⭕️ 栈。把 `StringBuilder` 当做栈来使用。将最后一个字符与当前字符比较,相等则删除最后一个字符,否则就添加。
480+
476481

477482
|===
478483

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1047_RemoveAllAdjacentDuplicatesInString {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-23 21:41:43
9+
*/
10+
public String removeDuplicates(String s) {
11+
// 使用 SB 当做栈,逐步推进
12+
// StringBuilder stack = new StringBuilder();
13+
// int top = -1;
14+
// for (int i = 0; i < s.length(); i++) {
15+
// char c = s.charAt(i);
16+
// if (top >= 0 && stack.charAt(top) == c) {
17+
// stack.deleteCharAt(top);
18+
// top--;
19+
// } else {
20+
// stack.append(c);
21+
// top++;
22+
// }
23+
// }
24+
// return stack.toString();
25+
// 同样的思路,直接在数组上操作。
26+
char[] chars = s.toCharArray();
27+
int top = -1;
28+
for (int i = 0; i < chars.length; i++) {
29+
if (top == -1 || chars[top] != chars[i]) {
30+
chars[++top] = chars[i];
31+
} else {
32+
top--;
33+
}
34+
}
35+
return new String(chars, 0, top + 1);
36+
}
37+
38+
// end::answer[]
39+
public static void main(String[] args) {
40+
new _1047_RemoveAllAdjacentDuplicatesInString()
41+
.removeDuplicates("abbbaca");
42+
}
43+
}

0 commit comments

Comments
 (0)