C++ | Leetcode C++题解之第225题用队列实现栈

题目:

题解:

class MyStack {
public:
    queue<int> q;

    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        int n = q.size();
        q.push(x);
        for (int i = 0; i < n; i++) {
            q.push(q.front());
            q.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int r = q.front();
        q.pop();
        return r;
    }
    
    /** Get the top element. */
    int top() {
        int r = q.front();
        return r;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return q.empty();
    }
};

相关推荐

  1. [力扣题解]225. 队列实现

    2024-07-10 07:50:06       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 07:50:06       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 07:50:06       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 07:50:06       90 阅读
  4. Python语言-面向对象

    2024-07-10 07:50:06       98 阅读

热门阅读

  1. 基于go 1.19的站点模板爬虫

    2024-07-10 07:50:06       30 阅读
  2. Pandas在生物信息学中的应用详解

    2024-07-10 07:50:06       29 阅读
  3. DOM XMLHttpRequest

    2024-07-10 07:50:06       26 阅读
  4. nginx详解

    2024-07-10 07:50:06       24 阅读
  5. vue实现表单输入框数字类型校验功能

    2024-07-10 07:50:06       39 阅读
  6. 【数据基础】— 基于Go1.19的站点模板爬虫的实现

    2024-07-10 07:50:06       34 阅读
  7. Perl 语言入门学习

    2024-07-10 07:50:06       34 阅读
  8. perl语言入门学习

    2024-07-10 07:50:06       34 阅读