import java.util.*;
import java.util.Stack;
public class Solution {
// 用于push的栈
Stack<Integer> stack1 = new Stack<Integer>();
// 用于pop的栈
Stack<Integer> stack2 = new Stack<Integer>();
// 进队
public void push(int node) {
stack1.add(node);
}
// 出队(重点)
public int pop() {
if (!stack2.isEmpty()) {
// 若pop栈非空,则直接弹出pop栈
return stack2.pop();
}
// 若pop栈空,则把push栈的内容依次转移到pop栈,再弹出
while (!stack1.isEmpty()) {
int temp = stack1.pop();
stack2.push(temp);
}
return stack2.pop();
}
}


京公网安备 11010502036488号