题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
思路:
1.用栈,但是最总还需要倒序输出
2.用可变长的数组来模拟栈(StringBuilder)
代码:
public static void main(String[]args){
Scanner in = new Scanner(System.in);
char[] s= in.next().toCharArray();
StringBuilder stack = new StringBuilder();
for(int i = 0;i<s.length;i++){
char c = s[i];
if(stack.length()!=0 && stack.charAt(stack.length()-1)==c){
//出栈
stack.deleteCharAt(stack.length()-1);
}else{
//进栈
stack.append(c);
}
}
System.out.println(stack.length()==0?0:stack.toString());
}