题解 | #点击消除#
点击消除
http://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
import java.math.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
public class Main {
static FastReader sc = new FastReader(); // 跟 scanner的使用读取方法一样
///////////////////////////////////////////////
////////////////////静态变量///////////////////////////
// static final int N = 100010;
// static int arr[] = new int[N];
///////////////////////////////////////////////
///////////////////////////////////////////////
//////////////////写解决方法/////////////////////////////
// 不需要 hasNext()
public static String solveCom() {
// int n = sc.nextInt();
String s = sc.next();
if (s.length() < 2) return s;
LinkedListStack<Character> stack = new LinkedListStack<>();
// 和 栈顶的比较一样的就不添加 不一样的就添加
stack.push(s.charAt(0));
for (int i = 1; i < s.length(); ++i) {
Character s_i = s.charAt(i);
Character top = stack.top();
if (top == s_i) {
stack.pop();
} else {
stack.push(s_i);
}
}
return stack.isEmpty() ? "0" : stack.printStack("", true);
} // solve fn ends
// t 次
public static void solveSub() {
int t = sc.nextInt();
while (t-- > 0) {
}
} // solve fn ends
// 需要 hasNext()
public static void solveScan() {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
while (scanner.hasNext()) {
}
}
///////////////////////////////////////////////
public static void main(String[] args) throws Exception {
//调用solve方法,好处是有多个题解可以写n个solve方法
System.out.println(solveCom());
// solveSub();
// solveScan();
}
///////////////////////////////////////////////
//////////////////////封装快速读取的方法/////////////////////////
//输出
static PrintWriter out;
//输入类
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
///////////////////////////////////////////////
}
/////////////////////链表
/**
* 栈的实现 -- 链表
* 优点: 使用灵活方便, 只要有需要的时候才会申请空间
* 缺点: 除了要存储元素外, 还需要额外存储指针(引用)信息
* @param <T>
*/
class Node<T> {
T data;
Node<T> next;
}
class LinkedListStack<T> {
private Node<T> pHead;
public LinkedListStack() {
pHead = new Node<T>();
pHead.data = null;
pHead.next = null;
}
public int size() {
int size = 0;
Node<T> cur = pHead.next;
while (cur != null) {
cur = cur.next;
size++;
}
return size;
}
public T pop() {
Node<T> popNode = pHead.next;
if (popNode == null) return null;
pHead.next = popNode.next;
return popNode.data;
}
public T top() {
Node<T> topNode = pHead.next;
if (topNode == null) return null;
return topNode.data;
}
public void push(T e) {
Node<T> tlNode = new Node<>();
tlNode.data = e;
tlNode.next = pHead.next;
pHead.next = tlNode;
}
public boolean isEmpty() {
return pHead.next == null;
}
public String printStack(String split_str, boolean isReverse) {
Node cur = pHead.next;
StringBuilder sb = new StringBuilder("");
while (cur != null) {
sb.append(cur.data + "");
sb.append(split_str);
cur = cur.next;
}
return isReverse ? sb.reverse().toString().substring(split_str.length()) : sb.toString().substring(0, sb.length() - split_str.length());
}
}
/////////////////////链表