LeetCode--232--用栈实现队列
问题描述:
使用栈实现队列的下列操作:
- push(x) -- 将一个元素放入队列的尾部。
- pop() -- 从队列首部移除元素。
- peek() -- 返回队列首部的元素。
- empty() -- 返回队列是否为空。
示例:
MyQueue queue = new MyQueue(); queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
- 你只能使用标准的栈操作 -- 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
方法:
class MyQueue(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.lists = [] def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.lists.append(x) def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
return self.lists.pop(0) def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.lists[0] def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return len(self.lists) == 0
官方:
闭
执行用时为 20 ms 的范例
class MyQueue(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.instack=[]
self.outstack=[] def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.instack.append(x) def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if self.outstack==[]:
while self.instack!=[]:
self.outstack.append(self.instack.pop())
return self.outstack.pop() def peek(self):
"""
Get the front element.
:rtype: int
"""
if self.outstack==[]:
while self.instack!=[]:
self.outstack.append(self.instack.pop())
return self.outstack[-1] def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return self.instack==[] and self.outstack==[]
2018-09-20 13:35:00
LeetCode--232--用栈实现队列的更多相关文章
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- Java实现 LeetCode 232 用栈实现队列
232. 用栈实现队列 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返 ...
- ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》
大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
- 力扣 - 232. 用栈实现队列.md
目录 题目 思路 代码实现 复杂度分析 题目 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列的支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void ...
- LeetCode刷题 --杂篇 --数组,链表,栈,队列
武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...
- leetcode刷题记录——栈和队列
题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...
- LeetCode入门指南 之 栈和队列
栈 155. 最小栈 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top( ...
- C#LeetCode刷题-栈
栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水 35.6% 困难 71 简 ...
- Go实现栈与队列基本操作
@ 目录 一 前言 二 实现栈与队列基本操作 2.1 栈基本操作 2.2 队列基本操作 三 用栈实现队列 3.1 理论 3.2 算法题 3.3 思路 3.4 代码部分 四 用队列实现栈 4.1 理论 ...
随机推荐
- 2018年11月22日 字典 E18灯翼平整度 D&G is SB
如果创建的东西需要增加修改的,则用list 如果不能修改就用元祖,如果需要修改这需要转成list 字典 字典的value是任意值 info= {"k1":'v1',"k2 ...
- django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件
一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...
- ng-model绑定的是ng-option中的什么?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- Android编译系统中的Android.bp【转】
本文转载自: 转自:http://note.qidong.name/2017/08/android-blueprint/ Android编译系统中的Android.bp.Blueprint与Soong ...
- centos6.5下安装jdk并配置环境变量
链接: https://blog.csdn.net/wawawawawawaa/article/details/81158943 以下链接供参考: https://blog.csdn.net/Bugg ...
- C# winform combobox默认选中项方法
https://blog.csdn.net/easyboot/article/details/68062196 可以使用 Combobox.SelectText = “默认选中文本”; 但是如果Com ...
- Docker、Kubenets使用前配置
1.开发人员需要确保机器上装有Docker并准确配置了Registry,能否推送相关镜像到Registry(运维人员无此要求) 2.能够访问Kubernetes APIServer相关API, 拥有相 ...
- Kubernetes之Controllers一
ReplicaSet is the next-generation Replication Controller. The only difference between a ReplicaSet a ...
- 51nod 1615 跳跃的杰克
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615 题意: 思路:一开始是觉得一旦超过了终点,中间某个地方往相反地方跳 ...
- Linux 命令之split(将一个大文件根据行数平均分成若干个小文件)
把一个 txt 文件导入到 excel 中,但是 excel 单列支持的行数为 1048576,而我需要导入的 txt 文件总共有 7945674 ,我们无法一次性将整个 txt 文件里面的内容导入到 ...