-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution1047.java
More file actions
executable file
·35 lines (32 loc) · 923 Bytes
/
Solution1047.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
import com.sun.deploy.util.StringUtils;
import java.util.Stack;
/**
* Created by slade on 2019/6/26.
*/
public class Solution1047 {
public String removeDuplicates(String S) {
Stack<Character> tmp = new Stack<>();
for (int i = 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();
}
}
/* 数据的展示可以继续优化 */
StringBuilder str = new StringBuilder();
for (Character c : tmp) {
str.append(c);
}
return str.toString();
}
public static void main(String[] args) {
Solution1047 s = new Solution1047();
String ss = "abbaca";
System.out.println(s.removeDuplicates(ss));
}
}