【小白爬Leetcode232】2.2 用栈实现队列 Implement Queue using Stacks


Leetcode 232 e s a y \color{#228B22}{esay} esay

点击进入原题链接:Leetcode225 用队列实现栈 Implement Stack using Queues

题目

Description

Implement the following operations of a queue using stacks.
· push(x) – Push element x to the back of queue.
· pop() – Removes the element from in front of queue.
· peek() – Get the front element.
· empty() – Return whether the queue is empty.

Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);  
queue.peek();  // returns 1
queue.pop();   // returns 1
queue.empty(); // returns false

Notes:

  1. You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is emptyoperations are valid.
  2. Depending on your language, stack may not be supported natively. You may simulate a stack by using a listor deque(double-ended queue), as long as you use only standard operations of a stack.
  3. You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

中文描述

使用栈实现队列的下列操作:
· push(x) – 将一个元素放入队列的尾部。
· pop() – 从队列首部移除元素。
· peek() – 返回队列首部的元素。
· empty() – 返回队列是否为空。

说明:

你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

思路一 用两个栈改变push的规则 【入队 - O(n), 出队 - O(1)】

核心思想在于让每次push元素的时候,都能push到栈底,这样栈就成了队列。

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        std::stack<int> temp;
        while(!_data.empty()){
            temp.push(_data.top());
            _data.pop();
        }
        temp.push(x);
        while(!temp.empty()){
            _data.push(temp.top());
            temp.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int res = _data.top();
        _data.pop();
        return res;
    }
    
    /** Get the front element. */
    int peek() {
        return _data.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return _data.empty();
    }
private:
    std::stack<int> _data; 
};

思路二 用两个栈动态储存信息 入队 - O(1),出队 - 摊还复杂度 【O(1)】

思路一的缺点在于每次push的时间复杂度都是n,而恰恰push是所有操作里最频繁的,因此效率较为低下。
而思路二的优势在于平均下来入队和出队的时间复杂度都为常数。它的实现思路是:
用两个stack s1s2

  1. 一开始队列为空,记录压入s1的第一个元素front,并把后续需要压入队列的元素压入s1;
  2. 当需要弹出元素的时候,s1不能实现弹出front的操作, 于是将s1的元素一个一个压入s2中,那么s2其实就是s1的反转,此时弹出s2的栈顶元素即完成了队列的pop操作;
  3. 像这样,新push的元素都进入s1pop的元素都从s2出,那么总有一天s2会耗尽。当s2耗尽,只需要重新将s1的元素弹入s2即可;
  4. 实现的队列的peek有两个可能:当s2不为空时,就是s2的栈顶元素;当s2为空时,则是记录的front变量。

复杂度分析:
时间复杂度: 入队O(1),只需要pushs1即可; 出队:假设pops1有n个元素,当s2为空的时候,需要将s1的元素弹出,再压入s2,复杂度为O(2n);当s2不为空的时候,只需要弹出s2的栈顶元素,复杂度为O(1),直到把s2pop完,复杂度为O(n-1)。总共进行了n次弹出操作,平均下来时间复杂度是O(3n-1/n)=O(1)。
空间复杂度: O(n),因为用了两个stack,这已经是最好的复杂度了。

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        if(s1.empty()) front = x;
        s1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int res;
        if(s2.empty()){
            while(!s1.empty()){
                res = s1.top();
                s2.push(res);
                s1.pop();
            }
        }
        res = s2.top();
        s2.pop();
        return res;
    }
    
    /** Get the front element. */
    int peek() {
        if(!s2.empty()) return s2.top();
        else return front;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty() && s2.empty();
    }
private:
    std::stack<int> s1;
    std::stack<int> s2; 
    int front;
};

结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值