【LeetCode】232.使用栈模拟队列
使用栈模拟队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
我的做法
当获取队首元素的时候就把栈1的元素出栈压入栈2的元素,压入的时候就直接压入栈1,有点蠢
public class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
while(!stack1.isEmpty()) {
var pop = stack1.pop();
stack2.push(pop);
}
var result = stack2.pop();
while(!stack2.isEmpty()) {
var pop = stack2.pop();
stack1.push(pop);
}
return result;
}
public int peek() {
while(!stack1.isEmpty()) {
var pop = stack1.pop();
stack2.push(pop);
}
var result = stack2.peek();
while(!stack2.isEmpty()) {
var pop = stack2.pop();
stack1.push(pop);
}
return result;
}
public boolean empty() {
return stack1.isEmpty();
}
}
代码随想录
使用两个栈,一个栈作为输入,另一个作为输出,获取队首的元素的时候判断输出栈是否为空,为空则将输入栈出栈压入输出栈,如此获取队首元素能够达到O(1),压入栈O(n)
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
【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() – 返 ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
- 剑指Offer9——使用双栈模拟队列
剑指Offer9--使用双栈模拟队列 队列Queue是具有FIFO(First in First out)特性的数据结构,栈Stack是具有LIFO(后进先出)特性的数据结构.下面提供一种思路使用双栈 ...
- 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...
- 模拟栈&&模拟队列
模拟栈:class Stack { private List list = new ArrayList( ); public void push( Object obj ) { this.list.a ...
- ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》
大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
- 力扣 - 232. 用栈实现队列.md
目录 题目 思路 代码实现 复杂度分析 题目 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列的支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void ...
- 剑指Offer05 用栈模拟队列
添加了模板类应用 /************************************************************************* > File Name: ...
- LeetCode刷题 --杂篇 --数组,链表,栈,队列
武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...
随机推荐
- 关于Python文件读取时,默认把\r\n换成\n
Python在非二进制形式读取文件时,自动把\r\n换成\n.(window下换行是\r\n) 建立一个test1.txt文件, aaaa bbbb 1.在utf8方式下读取 读取四个字符 1 f=o ...
- PyQt5 & PySide2信号与槽机制1
pyside2&pyqt5的信号与槽机制 1.信号与槽的两种写法 第一种情况: from PySide2 import QtWidgets, QtCore import sys if __na ...
- NOIP2014普及组
T2]比例简化 其实比较简单,主要是比较的方法以前没看过吧 要学会知识迁移啊! #include<iostream> #include<cstring> #include< ...
- 划分数据集时出现PermissionError: [Errno 13] Permission denied:
PermissionError: [Errno 13] Permission denied: [errno 13]权限被拒绝 错误的原因可能是文件找不到,或者被占用,或者无权限访问,或者打开的不是文件 ...
- What is RSS
What is RSS?RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many ...
- 贪心算法(Java)
贪心算法 文章目录 贪心算法 0.写在前面 1.贪心算法的基本要素 1.1 贪心选择性质 1.2 最优子结构性质 1.3 贪心算法与动态规划算法的差异 2.贪心算法的特点 3.贪心法的正确性证明 4. ...
- Leecode 1.两数之和(Java 哈希表)
想法: 1.哈希表hashmap 第一种方法:将数组中元素及其下标right都加入hashmap中,对于每个元素n下标left,在map中查找是否有target-n的元素,若有,则返回其下标right ...
- appium之手机操作的方法
Appium内置方法,来操作手机.在做app自动化时,可以内置方法的基本上,对一些常用的手机操作进行方法的封装. 常用的方法有: 获取手机分辨率: driver.get_window(size) # ...
- JS中报错处理 try catch finally的使用
JS中标准报错处理通过 try catch finally ,使用格式 try { } catch (err) { } finally { } 代码1: try { console.log('顺序 1 ...
- 用VUE框架开发的准备
使用VUE框架编写项目的准备工作 防止我几天不打代码,忘记怎么打了 下载小乌龟拉取码云项目文件,用于码云仓库代码提交与拉取(可以不安装) 小乌龟要设置你的码云账号 密码 在控制面版 中 凭证里可以修改 ...