题解 | #用两个栈实现队列#

用两个栈实现队列

http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6

A栈只管进,B栈只管出,B栈没有数据时,把A栈的全部拿过来再出

static Stack<integer> stack1 = new Stack<integer>();
static Stack<integer> stack2 = new Stack<integer>();</integer></integer></integer></integer>

public static void push(int node) {
    stack2.push(node);
}

public static int pop() {
    if(stack1.isEmpty()){
        int size = stack2.size();
        for (int i = 0; i < size; i++) {
            stack1.push(stack2.pop());
        }
    }
    return stack1.pop();
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务