LeetCode--225--用队列实现栈
问题描述:
使用队列实现栈的下列操作:
- push(x) -- 元素 x 入栈
- pop() -- 移除栈顶元素
- top() -- 获取栈顶元素
- empty() -- 返回栈是否为空
注意:
- 你只能使用队列的基本操作-- 也就是
push to back,peek/pop from front,size, 和is empty这些操作是合法的。 - 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
- 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
方法:
class MyStack(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.lists = []
def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
self.lists.append(x)
def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
if len(self.lists) == 0:
return
return self.lists.pop()
def top(self):
"""
Get the top element.
:rtype: int
"""
return self.lists[-1]
def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return len(self.lists) == 0
2018-09-19 15:07:25
LeetCode--225--用队列实现栈的更多相关文章
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
- [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列
##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- 领扣(LeetCode)用队列实现栈 个人题解
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- [LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...
- [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 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
随机推荐
- zw版【转发·台湾nvp系列Delphi例程】HALCON MaxImage1
zw版[转发·台湾nvp系列Delphi例程]HALCON MaxImage1 procedure TForm1.FormShow(Sender: TObject);begin Set8087CW($ ...
- vue的双向数据绑定原理
原理. vue是采用数据劫持结合发布者-订阅者模式的方式, 通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回 ...
- 使整个页面变灰的css代码
* { filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=); -webkit-filter: grayscale(%); - ...
- Java 执行jar文件出现版本错误信息
Java 执行jar文件出现版本错误信息 一.问题 执行jar文件出现如下错误信息: 二.解决方案 是因为在创建工程的时候选择的jdk编译版本,和执行jar环境的jdk版本不一致: 更改工程的jdk版 ...
- Mysql的序列
Mysql的序列 Mysql自带的序列:字段设置为int,属性里面选上“自动增长”即可: 在插入数据的时候可以不插入该字段的值,mysql会自动处理:
- Hive-查询结果导入到 MySQL
step1:add jar /home/chenweidong/lib/hive-contrib-2.1.1-cdh.0.0.jar;add jar /home/chenweidong/lib/mys ...
- 20145122《Android开发基础》实验四实验报告
实验名称 Android开发基础 实验内容 1.Windows环境下Android Studio 2.能够运行安卓AVD模拟器 3.使用安卓虚拟手机显示HelloWorld以及自己的学号 统计的PSP ...
- 20145301赵嘉鑫 《网络对抗》Exp9 Web安全基础实践
20145301赵嘉鑫 <网络对抗>Exp9 Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击原理:SQL 是一门 ANSI 的标准计算机语言,用来访 ...
- Email移动的原理
1.从数据库中得到被移动邮件的uid: 2.选择移动邮件所属folder,即SelectFolder; 3.调用copymessage(path,vmime::net::messageset::byU ...
- RNN网络【转】
本文转载自:https://zhuanlan.zhihu.com/p/29212896 简单的Char RNN生成文本 Sherlock I want to create some new thing ...