剑指offer - 面试题9:用两个栈实现队列 - C++

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        if(stack2.size() <= 0) {
            while(stack1.size() > 0) {
                int& top = stack1.top();  // pop不返回
                stack1.pop();
                stack2.push(top);
            }
        }
       /* if(stack2.size() <= 0) {
            return;
        } */
        int& top = stack2.top();
        stack2.pop();
        return top;
    }

private:
    stack stack1;
    stack stack2;
};

第三次过;

第一次:不了解模板中的stack,以为pop删除的同时可以返回栈顶元素;

第二次:if没有返回规定类型的值(写js习惯了...),这个问题以后一定避免:所有分支返回并返回合法值。另外不知道怎么throw error,参考讨论区代码好像没检测这个也过了。

另外:不知道引用声明 &是什么意思。

你可能感兴趣的:(剑指offer - 面试题9:用两个栈实现队列 - C++)