c++题解 用两个栈实现队列

用两个栈实现队列

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

用双栈实现队列

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

    int pop() {
        if(s2.empty()){
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }
        }
        if(s2.empty())
            return -1;
        int t = s2.top();
        s2.pop();
        return t;
    }

private:
    stack<int> s1;
    stack<int> s2;
};
全部评论

相关推荐

粗心的熊熊求求offer:什么内容都没有还弄两页
点赞 评论 收藏
分享
06-11 13:34
门头沟学院 C++
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-23 16:31
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务