题解 | #点击消除#
点击消除
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 的区别 while (in.hasNext()) { // 注意 while 处理多个 case Stack<Character> stack=new Stack<>(); String s=in.next(); char[] chars=s.toCharArray(); StringBuffer sBuffer=new StringBuffer(); for(int i=0;i<chars.length;i++){ if(stack.isEmpty()){ stack.push(chars[i]); sBuffer.append(chars[i]); } else if(chars[i]==stack.peek()){ stack.pop(); sBuffer.deleteCharAt(sBuffer.length()-1); }else{ stack.push(chars[i]); sBuffer.append(chars[i]); } } if(stack.isEmpty()){ System.out.println(0); } System.out.println(sBuffer); } } }