题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String s = in.nextLine();
Stack<Character> st = new Stack<>();
for(int i = 0;i < s.length();i++){
if(!st.isEmpty() && st.peek() == s.charAt(i)){
st.pop();
}else{
st.push(s.charAt(i));
}
}
if(st.isEmpty()){
System.out.printf("0");
}else{
StringBuilder sb = new StringBuilder();
while(!st.isEmpty()){
sb.append(st.pop());
}
System.out.printf(sb.reverse().toString());
}
}
}
