剑指offer 05:用两个栈实现队列

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

解题代码

import java.util.Stack;

public class Solution{
    Stack<Integer> in = new Stack<>();
    Stack<Integer> out = new Stack<>();
    
    public void push(int node){
        in.push(node);
    }
    
    public int pop(){
        if(out.isEmpty())
            while(!in.isEmpty())
                out.push(in.pop());
        return out.pop();
    }
}

 

posted @ 2019-04-15 13:10  MiaoPlus  阅读(132)  评论(0)    收藏  举报