[LeetCode] 232. Implement Queue using Stacks_Easy tag: Design
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:
- You must use only standard operations of a stack -- which means only
push to top,peek/pop from top,size, andis emptyoperations 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).
这个题目可以用两个stack, 基本思路是如@elmirap提出来的Solution, 两种方式, 第一种是O(n) push, 其他都是O(1)

Code:
class MyQueue(object):
##Solution
# two stacks, O(n) push operation def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.backup = [] def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
while self.stack:
self.backup.append(self.stack.pop())
self.stack.append(x)
while self.backup:
self.stack.append(self.backup.pop()) def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
return self.stack.pop() def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.stack[-1] def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.stack # Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
第二种方式, 平均pop为O(1), 其他都为O(1), 思路实际上类似, 但是在Stack2不急着将其搬回stack1, 因为在stack1搬到stack2之后已经将最后的放在最下面了.所以节约了时间.

Code:
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
self.queue = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.stack.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if not self.queue:
while self.stack:
self.queue.append(self.stack.pop())
return self.queue.pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.queue[-1] if self.queue else self.stack[0]
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.stack and not self.queue
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
[LeetCode] 232. Implement Queue using Stacks_Easy tag: Design的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [LeetCode] 225. Implement Stack using Queues_Easy tag: Design
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
随机推荐
- Python Tkinter Entry(文本框)
Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor e ...
- Android 全屏Activity以透明的对话框形式弹出
1. styles.xml <style name="transcutestyle" parent="@android:style/Theme.DeviceDefa ...
- sencha touch 在线实战培训 第一期 第五节
2014.1.6晚上8点准时开的课 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容: 实现皮肤自定义样式的修改 css相关理论: 重写官方样式,美化ap ...
- 完全卸载Oracle数据库软件
软件环境: 1.Windows xp+ORACLE 8.1.7 2.ORACLE安装路径为:C:\ORACLE 实现方法: 1. 开始->设置->控制面板->管理工具->服务 ...
- 关于ASP.NET Web API的ModelBinding杂谈
由于客户端调用Web API传递的数据属性命名一般偏向javascript规范,只是简单的大小写差异没有问题,但始终会有一些特殊情况.比如OAuth的请求: client_id : "val ...
- 解决jenkins下使用HTML Publisher插件后查看html报告显示不正常
在jenkins后使用html publisher查看html报告时,发现显示不全,很多东西显示不了. 在查看官方文档后,这原来是安全问题所导致的. Jenkins安全默认是将以下功能都关闭了 1.j ...
- springMVC 报错:Unknown return value type: java.lang.Integer
controller层返回值类型为Integer,运行报错: Unknown return value type: java.lang.Integer 解决办法:在此方法上写上注解 @Response ...
- PhoneGap安装配置
PhoneGap是一能够让你用普通NewsShow的web技术编写出能够轻松调用API接口和进入应用商店的HTML5应用开发平台.是唯一的一个支持7个平台的开源移动框架.它的优势是无以伦比的:开发成本 ...
- 9.11 Django视图 view和路由
2018-9-11 16:34:16 2018-9-11 19:00:24 越努力,.越幸运! Django框架参考: https://www.cnblogs.com/liwenzhou/p/8296 ...
- [分布式系统学习] 6.824 LEC3 GFS 笔记
Google File System 第三课的准备是阅读论文GFS.该论文是分布式系统中经典论文之一. 读完做一点小总结. GFS的feature 1. 非POXIS接口API,支持对文件和文件夹的创 ...