[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 ...
随机推荐
- JavaScript学习总结(七)
这一讲我们来学习DOM编程(十分重要),有了DOM编程,我们就可以操作任意的HTML元素了. DOM,文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建 ...
- Mybatis学习——Mybatis核心配置
MyBatis的核心配置 在使用MyBatis框架时,设计两个核心的d对象:SqlSessionFactory和SqlSession. SqlsessionFactory SqlSessionFact ...
- Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器
参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...
- c#之初识结构(Struct)
C# 结构(Struct) 首先结构是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构.通俗说:结构就是一个可以包含不同数据类型的集合.它是一种可以自 ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(上)
MyBatis从入门到精通(第9章):Spring集成MyBatis(上) Spring是一个为了解决企业级Web应用开发过程中面临的复杂性,而被创建的一个非常流行的轻量级框架. mybatis-sp ...
- Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 文件的关系
本教程针对于UEFI启动来叙述的,根据普遍的支持UEFI的机器来叙述. 标题简要说明:Windows Boot Manager --------安装完Windows系统后而出现的启动选项(相关的信息 ...
- CodeForces 996B World Cup(思维)
https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...
- java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/St ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- xcode6添加pch文件
pch文件 定义:该文件中定义的内容为全局变量,可供所有类进行调用 例子:在pch文件中定义ios版本