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 理论 ...
随机推荐
- 前端 --- 4 js
一.js 描述 JavaScript 是脚本语言 JavaScript 是一种轻量级的编程语言.后来出现了node.js,可以作为后端语言来开发项目, js是一个既能做前端又能做后端的语言 Java ...
- 做了一道cf水题
被一道cf水题卡了半天的时间,主要原因时自己不熟悉c++stl库的函数,本来一个可以用库解决的问题,我用c语言模拟了那个函数半天,结果还超时了. 题意大概就是,给定n个数,查询k次,每次查询过后,输出 ...
- 使用msi自动安装系统
在实际生活中, 还是要尽量使用 自动化 脚本 等来处理/执行问题, 那样更快更省力省时间 要多使用 网络工具, 网络工具在 管理/ 使用网络的过程 中还是很有用的. 要有这种 "多使用网络工 ...
- # bzoj2215: [Poi2011]Conspiracy 2-sat
bzoj2215: [Poi2011]Conspiracy 2-sat 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2215 思路 一个点的 ...
- SpringBoot Mybatis问题收集
1.在SpringBoot中打印mybatis中执行的sql 其实在application.properties 文件下,添加一下配置即可: logging.level.org.springframe ...
- hihoCoder week14 无间道之并查集
并查集的基本使用 #include <bits/stdc++.h> using namespace std; ; int n, fa[N]; int cnt, tot; map<st ...
- 【C#】C#学习笔记_1
C#的程序入口为某一个类里面的static void Main(string[] args){}方法,如果一个工程有多个Main方法,那么需要在工程配置中选择一个作为程序入口. C#的输入.输出操作在 ...
- eclipse创建maven web项目工程步骤示例
参考链接:https://www.cnblogs.com/noteless/p/5213075.html 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的ecli ...
- 解决 Boost安装:fatal error: bzlib.h: No such file or directory 问题
参考: How to install all the boost development libraries? 解决 Boost安装:fatal error: bzlib.h: No such fil ...
- sublime配置 sublimecondeintel 分号后不要提示
https://github.com/SublimeCodeIntel/SublimeCodeIntel/issues/461 Thanks to @catgsmith ,I find a simil ...