- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution1047.java
More file actions
Latest commit
executable file
·35 lines (32 loc) · 923 Bytes
/
Copy pathSolution1047.java
File metadata and controls
executable file
·35 lines (32 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importcom.sun.deploy.util.StringUtils;
importjava.util.Stack;
/**
* Created by slade on 2019/6/26.
*/
publicclassSolution1047 {
publicStringremoveDuplicates(StringS) {
Stack<Character> tmp = newStack<>();
for (inti = 0; i < S.length(); i++) {
if (tmp.empty()){
tmp.push(S.charAt(i));
continue;
}
if (S.charAt(i)!=tmp.peek()){
tmp.push(S.charAt(i));
}else {
tmp.pop();
}
}
/* 数据的展示可以继续优化 */
StringBuilderstr = newStringBuilder();
for (Characterc : tmp) {
str.append(c);
}
returnstr.toString();
}
publicstaticvoidmain(String[] args) {
Solution1047s = newSolution1047();
Stringss = "abbaca";
System.out.println(s.removeDuplicates(ss));
}
}