当前位置:网站首页>leetcode刷题:栈与队列01(用栈实现队列)
leetcode刷题:栈与队列01(用栈实现队列)
2022-07-01 19:16:00 【涛涛英语学不进去】
使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
- 你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
- 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
思路:就是两个栈嘛,一个用来接数据,一个用来出数据, 先进 后出 后进 先出。
入栈就直接入数据的栈 接数据 push了。
出栈和查看栈差不多,先看看出数据的栈是否是空的,如果是空的,把入数据的栈全推到出数据的栈, 先进 +( 后出 + 后进 )+ 先出 = 先进 先出
判空就更好做了,看看两个栈如果都是空才算空。
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName MyQueue * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 10:57 * @Version 1.0 * 232. 用栈实现队列 * https://leetcode.cn/problems/implement-queue-using-stacks/ * 思路: * 栈,先进后出 * 队列,先进先出 * 栈模拟队列,一个元素从第一个栈入,出来后再推入第二个栈,则 先进 后出 后进 先出 == 先进 先出 **/
public class MyQueue {
private Stack stackIn;
private Stack stackOut;
public MyQueue() {
stackIn = new Stack();
stackOut = new Stack();
}
/** * 推入一个元素 * * @param x */
public void push(int x) {
stackIn.push(x);
}
/** * 返回栈顶元素 * * @return */
public int pop() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
//老区先出完,新区再进
if (lenOut > 0) {
return (int) stackOut.pop();
} else {
//老区无元素,新区进老区
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.pop();
}
}
/** * 查看栈顶元素 * * @return */
public int peek() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
//老区先出完,新区再进
if (lenOut > 0) {
return (int) stackOut.peek();
} else {
//老区无元素,新区进老区
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.peek();
}
}
/** * 判空 * * @return */
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
}
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */

边栏推荐
-
- PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
- 极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
- Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
- 小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?
- #yyds干货盘点#SQL聚合查询方法总结
- ORA-01950
- 《软件工程导论(第六版)》 张海藩 复习笔记
- 薛定谔的日语学习小程序源码
- 深度学习 神经网络基础
猜你喜欢

Richview RichEdit srichviewedit PageSize page setup and synchronization

Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it

Items in richview documents

新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码

数据分析师听起来很高大上?了解这几点你再决定是否转型

Win11 how to hide the taskbar? Win11 method to hide the taskbar

NSI脚本的测试

收藏:存储知识全面总结

《軟件工程導論(第六版)》 張海藩 複習筆記

图片拼图微信小程序源码_支持多模板制作和流量主
随机推荐
Is it safe to open an account online? Can a novice open a stock trading account.
关联线探究,如何连接流程图的两个节点
Vulnerability recurrence - Net ueeditor upload
2022/5/23-2022/5/30
ORA-01950
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
Simple but modern server dashboard dashdot
寫博客文檔
实战项目笔记(一)——虚拟机的创建
Develop those things: easycvr cluster device management page function display optimization
C # joint Halcon application - Dahua camera acquisition class
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
Écrire un document de blog
升级版手机检测微信工具小程序源码-支持多种流量主模式
架构师毕业总结
How to turn off the boot auto start software in win11
Common components of flask
渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
fastDFS入门
基于图的 Affinity Propagation 聚类计算公式详解和代码示例