LeetCode第225题---用队列实现栈
发布日期:2021-05-10 10:38:52 浏览次数:17 分类:精选文章

本文共 2489 字,大约阅读时间需要 8 分钟。

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������:

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

������������������������������������������������������������������������������������������

  • ������������������������������x������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������x���������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������

    #include 
    using namespace std;
    class MyStack {
    private:
    queue
    q;
    public:
    void push(int x) {
    // ���������������������������������������
    int temp = 0;
    int size = q.size();
    while (size--) {
    temp = q.front();
    q.pop();
    }
    q.push(temp); // ������������������
    q.push(x); // ������������������
    }
    int pop() {
    return q.front();
    q.pop();
    }
    int top() {
    return q.front();
    }
    bool empty() {
    return q.empty();
    }
    };

    ���������������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ��������������������������������� O(1)������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:LeetCode第232题---用栈实现队列
    下一篇:牛客网---数组中出现次数超过一半的数字

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月04日 00时26分40秒