当前位置:网站首页>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(); */
边栏推荐
- [Mysql]安装Mysql5.7
- 漏洞复现-.Net-ueditor上传
- Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
- Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
- Iframe 父子页面通信
- 优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs
- Stack overflow 2022 developer survey: where is the industry going?
- [Blue Bridge Cup web] analysis of the real topic of the 13th Blue Bridge Cup web university group match in 2022
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- deb文件安装
猜你喜欢
Richview RichEdit srichviewedit PageSize page setup and synchronization
PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
Slf4j打印异常的堆栈信息
由浅入深学会白盒测试用例设计
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
Develop those things: easycvr cluster device management page function display optimization
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
How to create a pyramid with openmesh
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
随机推荐
走进如心小镇,数智化变革连接“未来社区”
On the usage of a magic function
【多线程】 实现单例模式 ( 饿汉、懒汉 ) 实现线程安全的单例模式 (双重效验锁)
【C语言】详解 memset() 函数用法
【let var const】
Big factories are wolves, small factories are dogs?
There are four ways to write switch, you know
《软件工程导论(第六版)》 张海藩 复习笔记
Slf4j打印异常的堆栈信息
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide
Is it safe to open an account online? Can a novice open a stock trading account.
在技术升级中迎合消费者需求,安吉尔净水器“价值战”的竞争之道
关于一个神奇函数的用法
牛客编程题--必刷101之字符串(高效刷题,举一反三)
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
2022安全员-A证考题及在线模拟考试
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
Internship: complex JSON format data compilation interface