题解 | #用两个栈实现队列#

用两个栈实现队列

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

#include <cstdio>
class Solution
{
public:
    void push(int node) {
     stack1.push(node);    ///  把栈 1 当做入栈 ,专门做入栈操作
    }

    int pop() {
       
       if(stack2.empty())     // Stack2作专门的出栈, 如果  Stack2  没有数据了, 就把Stack1全部转移过来,
       { 
            while(!stack1.empty()){
                stack2.push(stack1.top());
                stack1.pop();
            }

       }

       int ret = stack2.top();
        stack2.pop();
        return ret;
        
    }

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

全部评论

相关推荐

否极泰来来来来:解约赔多少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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