import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<>(); // 入队辅助栈
Stack<Integer> stack2 = new Stack<>(); // 出队辅助栈
// 模拟入队
public void push(int node) {
// 元素入队时直接压入栈1
stack1.push(node);
}
// 模拟出队
public int pop() {
// 元素出队时直接弹出栈2顶部元素
if (!stack2.isEmpty()) {
// 栈2不为空时直接弹栈
return stack2.pop();
} else {
// 栈2为空时则压入栈1全部元素
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
// 栈2弹栈
return stack2.pop();
}
}
}



京公网安备 11010502036488号