[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.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Example:
MyStack stack = new MyStack(); stack.push(1);
stack.push(2);
stack.top(); // returns 2
stack.pop(); // returns 2
stack.empty(); // returns false
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis 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).
利用如下图所示的方式, 每次push的时候将queue里面的值都放到x的后面即可.
Push: O(n), others : O(1)
Code
class MyStack(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.q = collections.deque() def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
size = len(self.q)
self.q.append(x)
while size:
self.q.append(self.q.popleft())
size -= 1 def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
return self.q.popleft() def top(self):
"""
Get the top element.
:rtype: int
"""
return self.q[0] def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return not self.q # Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
[LeetCode] 225. Implement Stack using Queues_Easy tag: Design的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- [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 ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
随机推荐
- Shell xargs
from here 简介之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: 这个命令是错误的find /sbin -perm + ...
- IPhoneX网页布局 全屏布局(转)
IPhoneX全面屏是十分科技化的,但是由于其圆角和摄像头刘海位置以及操控黑条的存在使得我们需要去对其样式做一些适配,没有X的同学可以开启 Xcode 9 的iPhone X 模拟器作为学习和调试. ...
- 题目1042:Coincidence(最长公共子序列 dp题目)
题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 2015.8.2js-19(完美运动框架)
/*完美运动框架*/ //1.先清除定时期,2,获取样式,如果是opacity则单独解决,3,定义速度,4,定义当前值是否到达目的地,5,判断当前值是否到达目的地,6运动基本,如果是opacity f ...
- wps插件开发中com组件权限
需要对wps写一个小的插件,也就是几行代码的事情,但却碰到了一个坑 wps中的com组件的调用和MSoffice非常的相似,几乎只需要把包的头修改一下就可以用了. 比如开发wps文档的插件,需要引用 ...
- CentOS7下Elastic Stack 5.0日志分析系统搭建
原文链接:http://www.2cto.com/net/201612/572296_3.html 在http://localhost:5601下新建索引页面输入“metricbeat-*”,之后ki ...
- Pexpect学习:
pexecpt run用法:格式:run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=Non ...
- PHPStorm 注册码&主题皮肤
JetBrains PhpStorm 注册方法: 用浏览器打开 http://idea.lanyus.com/ 点击页面中的“获得注册码” 然后打开PhpStorm,在注册时切换至Activation ...
- java常用数据格式转化,类似数据库group by cube rollup
java常用数据格式转化,类似数据库group by cube rollup单循环一条sql返回格式如:List<Map<String, List<Record>>> ...
- 170822、解决PLSQL记录被另一个用户锁住的问题
1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b. ...