使用栈模拟队列

​ 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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.使用栈模拟队列的更多相关文章

  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. LeetCode通关:栈和队列六连,匹配问题有绝招

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

  4. 剑指Offer9——使用双栈模拟队列

    剑指Offer9--使用双栈模拟队列 队列Queue是具有FIFO(First in First out)特性的数据结构,栈Stack是具有LIFO(后进先出)特性的数据结构.下面提供一种思路使用双栈 ...

  5. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...

  6. 模拟栈&&模拟队列

    模拟栈:class Stack { private List list = new ArrayList( ); public void push( Object obj ) { this.list.a ...

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

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

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

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

  9. 剑指Offer05 用栈模拟队列

    添加了模板类应用 /************************************************************************* > File Name: ...

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

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

随机推荐

  1. 中值滤波 ordfilt2函数

    滤波是过滤掉信号中没用的波段 ordfilt2函数语法格式为:B=ordfilt2(A,order,domain)B=ordfilt2(A,order,domain,S)B=ordfilt2(..., ...

  2. mysql和nacos都部署在docker中,ip该写哪个

    docker run -d \ -e MODE=standalone \ -e SPRING_DATASOURCE_PLATFORM=mysql \ -e MYSQL_SERVICE_HOST=172 ...

  3. 08 分布式计算MapReduce--词频统计

    def getText(): txt=open("D:\\test.txt","r").read() txt=txt.lower() punctuation = ...

  4. 实验一Linux系统与应用准备

    实验一Linux系统与应用准备 |这个作业属于哪个课程|内容| | ---- | ---- | ---- | |这个作业属于哪个课程|2021春季Linux系统与应用 (南昌航空大学 - 信息工程学院 ...

  5. StringIO 和 BytesIO

    StringIO 要把 str 字符串写入内存中,我们需要创建一个 StringIO 对象,然后像文件一样对读取内容.其中 StringIO 中多了一个 getvalue() 方法,目的是用于获取写入 ...

  6. PHP做API开发该如何设计签名验证

    前言 开发过程中,我们经常会与接口打交道,有的时候是调取别人网站的接口,有的时候是为他人提供自己网站的接口,但是在这调取的过程中都离不开签名验证. 我们在设计签名验证的时候,请注意要满足以下几点: 可 ...

  7. Honeywell安卓版手持机设置广播方式

    设置>Honeywell设置>扫描设置>Internal Scanner>Default profile>Data Processing Settings>Data ...

  8. 表格CSS

    .tab{border:1px solid #cad9ea;color:#666;} .tab th {background-image: url(th_bg1.gif);background-rep ...

  9. mssql实现Split

    create function Fun_Split( @SourceSql varchar ( 8000 ), @StrSeprate varchar ( 10 )) returns @temp ta ...

  10. C/S 架构 和 B/S 架构

    C/S架构的理解: 官方称:clinet-server 客户端需要下载的软件: 今日头条,爱奇艺等  能在手机和浏览器打开的软件 B/S架构的理解: 官方称:web-server 客户端为浏览器的: ...