Skip to content

Commit 45e286b

Browse files
committed
feat: add solutions to leetcode problem: No.1047
1 parent 9ff99f4 commit 45e286b

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,52 @@
3434

3535
<!-- 这里可写通用的实现逻辑 -->
3636

37+
栈实现。
38+
39+
遍历字符串 S 中的每个字符 s,若栈为空或者栈顶值不等于字符 s,s 入栈,否则栈顶元素出栈。
40+
41+
最后返回栈中元素所组成的字符串。
42+
3743
<!-- tabs:start -->
3844

3945
### **Python3**
4046

4147
<!-- 这里可写当前语言的特殊实现逻辑 -->
4248

4349
```python
44-
50+
class Solution:
51+
def removeDuplicates(self, S: str) -> str:
52+
res = []
53+
for s in S:
54+
if not res or res[-1] != s:
55+
res.append(s)
56+
else:
57+
res.pop()
58+
return ''.join(res)
4559
```
4660

4761
### **Java**
4862

4963
<!-- 这里可写当前语言的特殊实现逻辑 -->
5064

5165
```java
52-
66+
class Solution {
67+
public String removeDuplicates(String S) {
68+
StringBuilder sb = new StringBuilder();
69+
int top = -1;
70+
for (int i = 0, n = S.length(); i < n; ++i) {
71+
char s = S.charAt(i);
72+
if (top == -1 || sb.charAt(top) != s) {
73+
sb.append(s);
74+
++top;
75+
} else {
76+
sb.deleteCharAt(top);
77+
--top;
78+
}
79+
}
80+
return sb.toString();
81+
}
82+
}
5383
```
5484

5585
### **...**

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,37 @@ For example, in &quot;abbaca&quot; we could remove &quot;bb&quot; since the lett
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def removeDuplicates(self, S: str) -> str:
47+
res = []
48+
for s in S:
49+
if not res or res[-1] != s:
50+
res.append(s)
51+
else:
52+
res.pop()
53+
return ''.join(res)
4654
```
4755

4856
### **Java**
4957

5058
```java
51-
59+
class Solution {
60+
public String removeDuplicates(String S) {
61+
StringBuilder sb = new StringBuilder();
62+
int top = -1;
63+
for (int i = 0, n = S.length(); i < n; ++i) {
64+
char s = S.charAt(i);
65+
if (top == -1 || sb.charAt(top) != s) {
66+
sb.append(s);
67+
++top;
68+
} else {
69+
sb.deleteCharAt(top);
70+
--top;
71+
}
72+
}
73+
return sb.toString();
74+
}
75+
}
5276
```
5377

5478
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class Solution {
22
public String removeDuplicates(String S) {
3-
char[] cs = new char[S.length()];
3+
StringBuilder sb = new StringBuilder();
44
int top = -1;
5-
for (char c : S.toCharArray()) {
6-
if (top >= 0 && c == cs[top]) {
7-
--top;
5+
for (int i = 0, n = S.length(); i < n; ++i) {
6+
char s = S.charAt(i);
7+
if (top == -1 || sb.charAt(top) != s) {
8+
sb.append(s);
9+
++top;
810
} else {
9-
cs[++top] = c;
11+
sb.deleteCharAt(top);
12+
--top;
1013
}
1114
}
12-
return String.valueOf(cs, 0, top + 1);
15+
return sb.toString();
1316
}
14-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def removeDuplicates(self, S: str) -> str:
3+
res = []
4+
for s in S:
5+
if not res or res[-1] != s:
6+
res.append(s)
7+
else:
8+
res.pop()
9+
return ''.join(res)

0 commit comments

Comments
 (0)