博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Implement Queue using Stacks 用栈来实现队列
阅读量:4515 次
发布时间:2019-06-08

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

 

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.

Notes:

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

 

这道题让我们用栈来实现队列,之前我们做过一道相反的题目,是用队列来实现栈。这道题颠倒了个顺序,起始并没有太大的区别,栈和队列的核心不同点就是栈是先进后出,而队列是先进先出,那么我们要用栈的先进后出的特性来模拟出队列的先进先出。那么怎么做呢,其实很简单,只要我们在插入元素的时候每次都都从前面插入即可,比如如果一个队列是1,2,3,4,那么我们在栈中保存为4,3,2,1,那么返回栈顶元素1,也就是队列的首元素,则问题迎刃而解。所以此题的难度是push函数,我们需要一个辅助栈tmp,把s的元素也逆着顺序存入tmp中,此时加入新元素x,再把tmp中的元素存回来,这样就是我们要的顺序了,其他三个操作也就直接调用栈的操作即可,参见代码如下:

 

解法一:

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

 

上面那个解法虽然简单,但是效率不高,因为每次在push的时候,都要翻转两边栈,下面这个方法使用了两个栈_new和_old,其中新进栈的都先缓存在_new中,入股要pop和peek的时候,才将_new中所有元素移到_old中操作,提高了效率,代码如下:

 

解法二:

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

 

类似题目:

 

参考资料:

 

转载于:https://www.cnblogs.com/grandyang/p/4626238.html

你可能感兴趣的文章
pat乙级1036-1040
查看>>
Pyhton开发:Python基础杂货铺
查看>>
Springboot 打jar包分离lib,配置文件正确方式
查看>>
剑指Offer_编程题_18
查看>>
剑指Offer_编程题_23
查看>>
我所理解的 Laravel 请求 生命周期
查看>>
数组的合并
查看>>
ARC070F HonestOrUnkind
查看>>
最好的浏览器版本解析
查看>>
IIS发布问题集锦
查看>>
vue实例-学习
查看>>
微信开发从未如此简单-饭前甜点之公众号的“你问我答”
查看>>
C#发送邮件时提示:“不允许使用邮箱名称。服务器响应为:”的错误解决办法...
查看>>
约束 CONSTRAINT
查看>>
二分查找实现
查看>>
[网络收集]Session+Hashtable实现购物车
查看>>
js如何实动态删除DIV
查看>>
c++ new 堆 栈
查看>>
该好好学习英语了
查看>>
IPFS的配置安装
查看>>