zoukankan      html  css  js  c++  java
  • Leetcode 春季打卡活动 第一题:225. 用队列实现栈

    Leetcode 春季打卡活动 第一题:225. 用队列实现栈

    Leetcode 春季打卡活动 第一题:225. 用队列实现栈

    解题思路

    这里用了非常简单的思路,就是在push函数上做点操作,让队头总是最后一个元素即可。
    也就是说,每新进一个新元素,就把前面的所有元素逐个弹出放到队尾即可。

    Talk is cheap . Show me the code .

    class MyStack {
    public:
        queue<int> que;
        int size,temp;
        /** Initialize your data structure here. */
        MyStack() {
            size=0;
        }
        
        /** Push element x onto stack. */
        void push(int x) {
            size=que.size();
            que.push(x);
            while(size--){
                temp=que.front();
                que.pop();
                que.push(temp);
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        int pop() {
            if(!empty()){
                temp=que.front();
                que.pop();
                return temp;
            }
            return 0;
        }
        
        /** Get the top element. */
        int top() {
            if(!empty()) return que.front();
            return 0;
        }
        
        /** Returns whether the stack is empty. */
        bool empty() {
            return que.empty();
        }
    };
    
    /**
     * Your MyStack object will be instantiated and called as such:
     * MyStack* obj = new MyStack();
     * obj->push(x);
     * int param_2 = obj->pop();
     * int param_3 = obj->top();
     * bool param_4 = obj->empty();
     */
    
    作者:keep-smile
    链接:https://leetcode-cn.com/problems/implement-stack-using-queues/solution/bi-jiao-jian-dan-de-cao-zuo-fang-shi-rang-dui-tou-/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
  • 相关阅读:
    大学生自学网
    如何保证主从复制数据一致性
    CDN
    后端 线上 服务监控 与 报警 方案2
    利用 Gearman 实现系统错误报警功能
    增量部署和全量部署
    后端线上服务监控与报警方案
    简析TCP的三次握手与四次分手
    301 和 302 对 SEO 的影响
    Linux 查看负载
  • 原文地址:https://www.cnblogs.com/cell-coder/p/12389289.html
Copyright © 2011-2022 走看看