剑指offer---05---用栈实现队列

题意
给了两个栈去实现队列
 
分析
两个栈如下情况
      1
      2
4    3
这个时候就不能够把4插入到第二个弹出栈了否则弹出顺序出错。
所以这个时候就应该等第二个栈空了的时候再弹出。
 
代码
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()){
                int node = stack1.pop();
                stack2.push(node);
            }
        }
        return stack2.pop();
    }
}

转载于:https://www.cnblogs.com/buptyuhanwen/p/9376929.html

原文地址:https://www.cnblogs.com/twodog/p/12136366.html