LeetCode225:Implement Stack using Queues
来源:互联网 发布:vm 网络上有重名 编辑:程序博客网 时间:2024/05/20 12:21
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue – which means only
push to back, peek/pop from front, size, and is empty operations are
valid. - Depending on your language, queue may not be supported natively. You
may simulate a queue by using a list or deque (double-ended queue),
as long as you use only standard operations of a queue. You may
assume that all operations are valid (for example, no pop or top
operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
Hide Tags Stack Data Structure
这道题是让我们用队列来实现一个栈。
并且只使用队列中的push(),pop(),front(),empty(),size()函数。
关于队列的介绍,可以查看cpluscplus中queue的介绍。队列是一种先进先出(FIFO,first-in first-out)的容器适配器,元素被插入到容器的尾端并且从另一端弹出。
队列是作为容器适配器来实现的,容器适配器是指封装了一个特定的容器类的对象作为底层数据结构的类,它提供了一些特殊的操作来访问它的元素。元素被插入容器的尾端(back),并且从它的前端(front)弹出。
因为之前见过使用栈来实现队列,它是使用两个栈来实现队列,于是就想能不能使用两个队列来实现栈,结果发现还真是可以。定义两个队列作为自定义栈的成员变量,当需要插入元素时找到不为空的那个队列将元素插入进行,弹出元素时将不为空的那个队列的前n-1(n为队列中元素的大小)的元素弹出并插入为空的那个队列中,再弹出最后一个元素,这样插入的时间复杂度是O(1),弹出最后一个元素和获取最后一个元素的时间复杂度是O(n),图示如下:
C++代码如下:
runtime:0ms
class Stack {public: // Push element x onto stack. void push(int x) { if(queueOne.empty()) { queueTwo.push(x); }else { queueOne.push(x); } } // Removes the element on top of the stack. void pop() { if(queueOne.empty()) { while(queueTwo.size()!=1) { queueOne.push(queueTwo.front()); queueTwo.pop(); } queueTwo.pop(); } else { while(queueOne.size()!=1) { queueTwo.push(queueOne.front()); queueOne.pop(); } queueOne.pop(); } } // Get the top element. int top() { if(queueOne.empty()) { while(queueTwo.size()!=1) { queueOne.push(queueTwo.front()); queueTwo.pop(); } int result=queueTwo.front(); queueOne.push(queueTwo.front()); queueTwo.pop(); return result; } else { while(queueOne.size()!=1) { queueTwo.push(queueOne.front()); queueOne.pop(); } int result=queueOne.front(); queueTwo.push(queueOne.front()); queueOne.pop(); return result; } } // Return whether the stack is empty. bool empty() { return queueOne.empty()&&queueTwo.empty(); } private: queue<int> queueOne; queue<int> queueTwo;};
但是在Discuss中看到一种更好的解法,它只需要使用一个队列就可以了。并且代码量大大减少了。
具体做法每往队列中插入一个元素,都将它前面的元素弹出并重新插入队列中,这样就能保证最后插入队列的元素始终在队列的最前端,比如插入a,b,c,d这四个元素,队列中元素的顺序依次为a,ab,abc,abcd,这样插入的时间复杂度是O(n),弹出和获取最后一个元素的时间复杂度是O(1),是不是很奇妙?图示如下:
C++代码如下:
runtime:0ms
class Stack {public: // Push element x onto stack. void push(int x) { q.push(x); for(int i=0;i<q.size()-1;i++) { q.push(q.front()); q.pop(); } } // Removes the element on top of the stack. void pop() { q.pop(); } // Get the top element. int top() { return q.front(); } // Return whether the stack is empty. bool empty() { return q.empty(); } private: queue<int> q;};
- LeetCode225:Implement Stack using Queues
- leetcode225 Implement Stack using Queues
- LeetCode225. Implement Stack using Queues
- Leetcode225. Implement Stack using Queues
- leetcode225.Implement Stack using Queues
- LeetCode225——Implement Stack using Queues
- LeetCode225—Implement Stack using Queues
- LeetCode225 Implemet Stack using Queues Java题解
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack using Queues
- mysql 随机更新表里的几条数据
- 我开通新的博客啦!
- 轻松几步,让Visual Stdio 2010 支持Html5
- ASP.NET-DataList控件-DataList嵌套
- 设计模式读书笔记:Mediator(中介者)
- LeetCode225:Implement Stack using Queues
- 多个文本文件两两碰撞相同内容
- 数据类型和运算符(三)
- 移动开发者们,是时候使用HTML5了!
- JAVA中求解对象所占字节大小
- 继承(代码体现)
- php mysql_fetch_array与mysql_fetch_assoc区别
- 质数筛选方法(埃拉托斯特尼筛法)
- [Solved] MATLAB Linux install error: /tmp/mathworks_15227/java/jre/glnxa64/jre/bin/java: Permission