问题描述:

使用栈实现队列的下列操作:

  • 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 toppeek/pop from topsize, 和 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--用栈实现队列的更多相关文章

  1. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  2. Java实现 LeetCode 232 用栈实现队列

    232. 用栈实现队列 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返 ...

  3. ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》

    大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...

  4. LeetCode通关:栈和队列六连,匹配问题有绝招

    刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...

  5. 力扣 - 232. 用栈实现队列.md

    目录 题目 思路 代码实现 复杂度分析 题目 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列的支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void ...

  6. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  7. leetcode刷题记录——栈和队列

    题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...

  8. LeetCode入门指南 之 栈和队列

    栈 155. 最小栈 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top( ...

  9. C#LeetCode刷题-栈

    栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水   35.6% 困难 71 简 ...

  10. Go实现栈与队列基本操作

    @ 目录 一 前言 二 实现栈与队列基本操作 2.1 栈基本操作 2.2 队列基本操作 三 用栈实现队列 3.1 理论 3.2 算法题 3.3 思路 3.4 代码部分 四 用队列实现栈 4.1 理论 ...

随机推荐

  1. 如果此表在它的 ChildRelation 集合中不是父表,则不能将关系添加到该集合中。

    今天遇到这个问题头都大了,百度上也没找到解决方案,就自己在哪里沉思................ 终于皇天不负有心人,被我解决了! 这是调用ChildRelations.Add(“名字”,“父级”, ...

  2. L2-001:dijskstra + 多条最短路径 + 记录中间路径

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 思路: dijkstra算出最短路 ...

  3. html 之 区块元素属性(待补充)

    区块标签:(会随着父类的宽度变化而变化) p,div 等(后续补充) 区块标签才能使用以下属性() width,height,min-height,max-height,min-width,max-h ...

  4. UVA11417 GCD

    题目地址 题目链接 题解 先讨论任何没有限制的情况 \[ \large { \begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j)\\ &a ...

  5. HIHOcoder 1403 后缀数组一·重复旋律

    思路 后缀数组的板子题,注意后缀数组的rank[]数组是通过位置找到对应排名的,sa[]是通过排名找到位置的,height[i]记录的是sa[i]和sa[i+1]之间的lcp 不要写错了就行了 代码 ...

  6. facebook api之Access Tokens之Business Manager System User

    Business Manager System User Make programatic, automated actions on ad objects or Pages, or do progr ...

  7. hadoop的Linux操作

    初学hadoop之linux系统操作的hdfs的常用命令 Hadoop之HDFS文件操作 Hadoop fs命令详解 官网doc sudo su - hdfs:免密,以hdfs账户登陆.可操作hdfs ...

  8. strlen函数,strcat函数,strcpy函数,strncpy函数,strcmp函数

    strcpy函数: char *strcpy(char *Dest , const char *Src) { assert((Dest != NULL) && (Src != NULL ...

  9. ssh中的 Connection closed by ***

    另一台电脑的 mac/windows10/win7 都可以连接,就这台电脑不可以,但是能 ping 通, ssh 时总是 Connection reset by xxx 或 Connection cl ...

  10. Javascript 高级程序设计(第3版) - 第02章

    2017-05-10 更新原文: http://www.cnblogs.com/daysme 在 html 中使用 js 把js代码写在 <script type="text/java ...