题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Deque<Character> stack = new ArrayDeque<>(); char[] chars = br.readLine().toCharArray(); for (char c : chars) { if (!stack.isEmpty() && stack.peek() == c) { stack.pop(); } else { stack.push(c); } } StringBuilder sb = new StringBuilder(); if(stack.isEmpty()){ System.out.println(0); } while (!stack.isEmpty()) { sb.append(stack.removeLast()); } System.out.println(sb); } }