import java.util.*; import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if(stack2.isEmpty()) { while(!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } }
用两个栈来模拟队列,一个栈它的输出顺序是反着的,那么我们就可以让这个栈里面的数据全部出栈进入到另一个栈,然后再从另一个栈中pop数据,也就是负负得正