剑指offer05

109 阅读1分钟

题目描述

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

解题思路分析

这个题也很简单真的其实,不需要太多的解析,看代码就马上能懂了

题目源代码

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

    public static int pop() {
        if (stack2.size() == 0) {
            if (stack1.size() == 0) {
                return -1;
            } else {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
        }
        return stack2.pop();
    }