剑指offer--3.用两个栈实现队列

 快速刷一遍,先捏软柿子

-----------------------------------------------------------------

时间限制:1秒 空间限制:32768K 热度指数:366405
本题知识点: 队列 

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
 
class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        while(!stack1.empty()) {
            int tmp = stack1.top();
            stack2.push(tmp);
            stack1.pop();
        }
        int res = stack2.top(); stack2.pop();
        while(!stack2.empty()) {
            int tmp = stack2.top();
            stack1.push(tmp);
            stack2.pop();
        }
        return res;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};
原文地址:https://www.cnblogs.com/langyao/p/10562695.html