[LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues
难度:简单
通过率:59.9%
题目描述:
使用队列实现栈的下列操作:
- push(x) -- 元素 x 入栈
- pop() -- 移除栈顶元素
- top() -- 获取栈顶元素
- empty() -- 返回栈是否为空
注意:
- 你只能使用队列的基本操作-- 也就是
push to back,peek/pop from front,size, 和is empty这些操作是合法的。 - 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
- 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
示例:
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.top(); // returns 2
stack.pop(); // returns 2
stack.empty(); // returns false
思路:
栈:先进后出
队列:先进先出
用队列 模拟 栈
直接看代码, 有例子模拟一下,就能理解!
如有疑惑,欢迎留言~
代码:
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
from collections import deque
self.queue = deque()
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.queue.append(x)
for _ in range(len(self.queue) - 1):
self.queue.append(self.queue.popleft())
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
return self.queue.popleft()
def top(self) -> int:
"""
Get the top element.
"""
if self.queue:
return self.queue[0]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return not bool(self.queue)
[LeetCode] 226. 用队列实现栈的更多相关文章
- 领扣(LeetCode)用队列实现栈 个人题解
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- Java实现 LeetCode 225 用队列实现栈
225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...
- [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列
##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- 【leetcode 简单】 第六十三题 使用队列实现栈
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...
- C#LeetCode刷题之#225-用队列实现栈(Implement Stack using Queues)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4106 访问. 使用队列实现栈的下列操作: push(x) -- ...
随机推荐
- #4 div1E Parentheses 括号匹配
E - Parentheses Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Subm ...
- tensorboard的log查看方法
使用:tensorboard --logdir=D:\LOG logs --host=127.0.0.1
- ACM技能表
看看就好了(滑稽) 数据结构 栈 栈 单调栈 队列 一般队列 优先队列/单调队列 循环队列 双端队列 链表 一般链表 循环链表 双向链表 块状链表 十字链表 邻接表/邻接矩阵 邻接表 邻接多重表 Ha ...
- ubuntu E: Could not get lock /var/lib/apt/lists/lock 异常信息
转载:https://www.cnblogs.com/qq952693358/p/6537846.html 在更换软件源时遇到了如下问题: sudo apt-get update E: Could n ...
- MariaDB 默认是禁止远程访问的 我们改掉它
查询用户账号信息: select User, host from mysql.user; 现在只显示 root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限,输入 ...
- 并发量,tps,qps
QPS/TPS/并发量/系统吞吐量的概念 2017年08月13日 17:24:47 阅读数:10682 我们在日常工作中经常会听到QPS/TPS这些名词,也会经常被别人问起说你的系统吞吐量有多大.这个 ...
- html外部文件读取/写入
1.文件的读取 外部文件读取控件: <input type="file" id="file_jquery" onchange="file_jqu ...
- vue.js环境安装
1.到官网(http://nodejs.cn/download/)下载Node.JS运行环境并安装(由于现在的Node中自带npm包管理器,所以就不需要额外下载npm了) 2.如果是新手,那么建议以引 ...
- sonarqube修改自己的图像avatar
https://community.sonarsource.com/t/how-can-i-change-my-avatar/11457/2 Hi, User icons are provided b ...
- JSP——指令(Directive)
指令是一种JSP句法元素,它告诉JSP转换器应该如何将某个JSP页面转换成Servlet. JSP 2.2中定义了几个指令:page.include.taglib.tag.attribute以及var ...