添加1047.删除字符串中的所有相邻重复项 Java 版本

This commit is contained in:
nanhuaibeian
2021-05-13 09:40:40 +08:00
committed by GitHub
parent 7c8cb23ce5
commit 02e9ed76bf

View File

@ -122,7 +122,28 @@ public:
Java
```Java
class Solution {
public String removeDuplicates(String S) {
Deque<Character> deque = new LinkedList<>();
char ch;
for (int i = 0; i < S.length(); i++) {
ch = S.charAt(i);
if (deque.isEmpty() || deque.peek() != ch) {
deque.push(ch);
} else {
deque.pop();
}
}
String str = "";
//剩余的元素即为不重复的元素
while (!deque.isEmpty()) {
str = deque.pop() + str;
}
return str;
}
}
```
Python