-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1047.java
More file actions
27 lines (26 loc) · 802 Bytes
/
LeetCode1047.java
File metadata and controls
27 lines (26 loc) · 802 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
public class LeetCode1047 {
public static void main(String[] args) {
String S1, S2;
// 输入:"abbaca"
// 输出:"ca"
S1 = "abbaca";
System.out.println(S1);
S2 = new Solution1047().removeDuplicates(S1);
System.out.println(S2);
}
}
class Solution1047 {
public String removeDuplicates(String s) {
StringBuilder stack = new StringBuilder();
int length = s.length();
stack.append(s.charAt(0));
for (int i = 1; i < length; i++) {
if (!stack.isEmpty() && stack.charAt(stack.length() - 1) == s.charAt(i)) {
stack.deleteCharAt(stack.length() - 1);
} else {
stack.append(s.charAt(i));
}
}
return stack.toString();
}
}