当前位置:网站首页>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(); */

边栏推荐
- 基于图的 Affinity Propagation 聚类计算公式详解和代码示例
- Items in richview documents
- [Mysql]安装Mysql5.7
- Set object value changes null value object
- C # joint Halcon application - Dahua camera acquisition class
- 宅男壁纸大全微信小程序源码-带动态壁纸支持多种流量主
- Keras machine translation practice
- 天气预报小程序源码 天气类微信小程序源码
- Develop those things: easycvr cluster device management page function display optimization
- Keras机器翻译实战
猜你喜欢

STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration

【Leetcode】最大连续1的个数

300题线性代数 第四讲 线性方程组

喜马拉雅自研网关架构演进过程

math_利用微分算近似值

强大的万年历微信小程序源码-支持多做流量主模式

RichView RichEdit SRichViewEdit PageSize 页面设置与同步

Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)

Slf4j打印异常的堆栈信息

Entering Ruxin Town, digital intelligence transformation connects "future community"
随机推荐
How to turn off the boot auto start software in win11
2022安全员-B证考试练习题模拟考试平台操作
Arduino Stepper库驱动28BYJ-48步进电机测试程序
寫博客文檔
Summary of SQL aggregate query method for yyds dry goods inventory
运动捕捉系统原理
Keras machine translation practice
C # joint halcon Application - Dahua Camera Collection class
#yyds干货盘点#SQL聚合查询方法总结
switch 有四样写法你知道么
internship:逐渐迈向项目开发
2022/6/8-2022/6/12
考虑关系的图卷积神经网络R-GCN的一些理解以及DGL官方代码的一些讲解
优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs
Problems encountered in installing MySQL in docker Ubuntu container
ORA-01950
三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
Entering Ruxin Town, digital intelligence transformation connects "future community"
Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
Use Zadig to build a continuous delivery platform from 0 to 1