[LC] 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Example:
MyQueue queue = new MyQueue(); queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
Notes:
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class MyQueue: def __init__(self):
"""
Initialize your data structure here.
"""
self.stack_in, self.stack_out = [], [] def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.stack_in.append(x) def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
self._transfer()
return self.stack_out.pop() def peek(self) -> int:
"""
Get the front element.
"""
self._transfer()
return self.stack_out[-1] def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return not self.stack_in and not self.stack_out def _transfer(self):
if not self.stack_out:
while self.stack_in:
self.stack_out.append(self.stack_in.pop()) # Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
[LC] 232. Implement Queue using Stacks的更多相关文章
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 【LeetCode】232. Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- 【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
随机推荐
- 100道Java面试题整理(助力2020面试!)
1.您对微服务有何了解? 微服务,又称微服务 架 构,是一种架构风格,它将应用程序构建为以业务领域为模型的小型自治服务集合 . 通俗地说,你必须看到蜜蜂如何通过对齐六角形蜡细胞来构建它们的蜂窝状物.他 ...
- 3D打印前途光明,它需要怎样的进化?
在很长一段时间内,笔者都认为3D打印只会存在于科幻场景内,众多的科技大佬在前几年也和我保持相当一致的看法,代工大王郭台铭曾口出狂言:如果3D打印能够普及,我就把"郭"字倒过来写,时 ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- Django整体架构
Django整体架构 用户能够访问到的所有的资源 都是程序员提前暴露好的, 如果没有暴露 用户就永远访问不了 用户能够访问到的所有的资源 都是程序员提前暴露好的, 如果没有暴露 用户就永远访问不了 一 ...
- c#为什么要用事物
一.事务的定义 所谓事务,它是一个操作集合,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.典型的例子就像从网上银行系统的帐户A转帐到帐户B,它经过两个阶段:1.从帐户A取出款项.2.把 ...
- zabbix3.4--配置微信告警
1.注册企业微信 https://work.weixin.qq.com/ 2.注册好后登陆,点击“我的企业”,记录企业ID. 3.点击“应用管理”--“创建应用”,创建应用时添加接收告警的用户 4.添 ...
- jest 测试入门(一)
说实话,作为前端来说,单元测试,并不是一种必须的技能,但是确实一种可以让你加法的技能 之前我一个库添加了单元测试,加完之后感悟颇深,所以写下这篇文章来记录 环境搭建 一般来说,普通的库,如果没有添加 ...
- oracle 导入导出参数
- layui子弹框调用父弹框方法
var thisFrame = parent.window.document.getElementById("LAY_layuiStampDuty1").getElementsBy ...
- windows 安装MySQL服务 zip解压程序
1:配置 my.ini 文件 如下: [mysql] default-character-set=utf8[mysqld] port=3306basedir=D:\\Program Files\\da ...