添加1047.删除字符串中的所有相邻重复项 Java 版本
This commit is contained in:
parent
2e2f7735d6
commit
4a582a32fb
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue