思路:
加入队列:将加入队列的数全部压入stack1;
弹出队列:
1.将stack1的数依次弹出并压入stack2;
2.stack2弹出结果;
3.将stack2的数依次弹出并压入stack1;
java代码如下:
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() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
int res=stack2.pop();
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return res;
}
}