题解:#二个栈实现一个队列#

https://www.nowcoder.com/share/jump/5876869471694575816508

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

    }

    int pop() {
        // int data= stack1.top();
        // stack1.pop();
        // stack2.push(data);
        while (!stack1.empty()) {
           int data= stack1.top();
            stack1.pop();
            stack2.push(data);
        }
        //这样做是因为输入案列需要一个一个pop所以剩下的都需要返回到stack中
        int res = stack2.top();
        stack2.pop();
        while (!stack2.empty()) {
            stack1.push(stack2.top());
            stack2.pop();
        }
        return res;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

#剑指offer#
剑指offer刷题 文章被收录于专栏

坚持!努力!学习

全部评论

相关推荐

头像
02-15 16:23
中南大学 Java
野猪不是猪🐗:签了美团真是不一样! 亲戚们都知道我签了美团,过年都围着我问送一单多少钱,还让弟弟妹妹们引以为戒,笑我爸我妈养了个🐢孩子,说从小就知道我这个人以后肯定没出息,我被骂的都快上天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务