当前位置:网站首页>记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
2022-06-25 22:06:00 【程の编程日记】
刷题这些天,每天都靠看题解过日子,今天又随机了一道题,用两个栈实现队列,看题目感觉挺不错的,但是看输入输出的样例突然就懵了,我感觉这道题可能就是题目看着比较麻烦其实做起来不麻烦,下面是题:
题目链接:用两个栈实现队列
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
输出:[null,null,3,-1]
示例 2:
输入:
[“CQueue”,“deleteHead”,“appendTail”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]
提示:
1 <= values <= 10000
最多会对 appendTail、deleteHead 进行 10000 次调用
思路也很简单,就是两个栈,a栈就用来存放入队的东西,出队的时候,把a栈里的东西都依次弹出,压入b栈,然后b栈再出栈,就是先了出队,也就是实现了先进先出,那么出队一个数的话,再次入队还是往a栈里面放,当b出完之后(为空),再把a栈里面的依次弹出压入b栈。
总的来说的就是,a栈用来放入队的值,b栈用来放出队的值,b栈为空的时候把a栈里面的值依次弹出压入b栈即可。
下面是代码,也是纪念以下自己瞬间就有思路的题吧…
import java.util.Stack;
public class CQueue {
Stack<Integer> a;
Stack<Integer> b;
public CQueue() {
// 找两个栈,一个栈存数据,另一个栈作出队操作
a = new Stack<>();
b = new Stack<>();
}
public void appendTail(int value) {
// 入队操作
a.push(value);
}
public int deleteHead() {
if (b.empty()){
int size = a.size();
// b栈为空,则把a里面东西放里头操作
for (int i = 0; i < size; i++){
b.push(a.pop());
}
}
try{
Integer pop = b.pop();
if (pop != null) return pop;
}catch (Exception e){
return -1;
}
return -1;
}
}
边栏推荐
- Kylin
- Style setting when there is a separator in the qcombobox drop-down menu
- Anaconda一文入门笔记
- Leetcode-1528- rearrange string - hash table - string
- Online customer service - charging standards and service provision of third parties
- 二叉排序树
- MySQL InnoDB锁知识点
- idea 查看单元测试覆盖率
- DPVS-FullNAT模式部署篇
- Analysis on the control condition and mode of go cooperation overtime exit
猜你喜欢

一文讲透研发效能!您关心的问题都在

C# IO Stream 流(一)基础概念_基本定义

18亿像素火星全景超高清NASA放出,非常震撼

Online customer service - charging standards and service provision of third parties

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or

Analyse des cinq causes profondes de l'échec du développement de produits

unsigned与signed之大白话
![Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]](/img/3a/fb3bfe673db5123e3124404f3905c0.png)
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]

平衡二叉树AVL

流数据
随机推荐
提取系统apk
util. Collection and encapsulation of JS tool functions
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
Extraction system apk
proxy
先序线索二叉树
Uniapp -- list page of multi header tabs
Understanding of pseudo classes
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
谈一谈PHP变量或参数的Copy On Write机制
社招两年半10个公司28轮面试面经(含字节、拼多多、美团、滴滴......)
sqlServer2008中float类型数据与datetime类型数据互转
使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
解析产品开发失败的5个根本原因
Hibernate entity class curd, transaction operation summary
如何设计产品的roadmap?
Hbuilderx uses the gaude map to obtain the current location
UE4 learning record 2 adding skeleton, skin and motion animation to characters
Solve 'tuple' object has no attribute 'lower‘