题解 | #点击消除#
点击消除
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); while (in.hasNextLine()) { // 注意 while 处理多个 case String str = in.nextLine(); Stack<Character> stack = new Stack<>(); for(int i = 0; i < str.length(); i++){ char ch = str.charAt(i); if(stack.isEmpty() || stack.peek() != ch){ stack.push(ch); }else { stack.pop(); } } //栈当中存储好了数据 if(stack.empty()){ System.out.print(0); }else{ Stack<Character> stack2 = new Stack<>(); while(!stack.empty()){ stack2.push(stack.pop()); } while(!stack2.empty()){ System.out.print(stack2.pop()); } } } } }