当前位置:网站首页>由两个栈组成的队列
由两个栈组成的队列
2022-06-28 13:46:00 【华为云】
由两个栈组成的队列
【题目】
编写一个类,用两个栈实现队列,支持队列的基本操作(add、poll、peek)。
【思路】
前景知识:栈,先进后出;队列,先进先出
两个栈实现队列,用两个栈就是为了让栈中的数据反转一次,数据如a栈,把a栈的数据都pop出来push到b栈,这样a栈栈底的东西就会到b栈的栈顶了,有两个前提:
- a栈往b栈压数据时,必须把a栈中数据全部压入b栈
- 只有b栈为空,才能往b栈压入数据

【代码】
package keafmd.accumulate.codeinterviewguide.twostacksformaqueue;import java.util.Stack;/** * Keafmd * * @ClassName: MyStack1 * @Description: 两个栈实现的队列 add、poll、peek * @author: 牛哄哄的柯南 * @date: 2022-06-21 17:38 */public class TwoStacksQueue { Stack<Integer> stackA; Stack<Integer> stackB; public TwoStacksQueue(){ stackA = new Stack<>(); stackB = new Stack<>(); } //转移 a栈的数据全部倒入b栈 public void transfer(){ if(stackB.isEmpty()){ while(!stackA.isEmpty()){ stackB.push(stackA.pop()); } } } public void add(Integer val){ stackA.push(val); transfer(); } public Integer poll(){ if(stackA.isEmpty()&&stackB.isEmpty()){ throw new RuntimeException("Queue is empty!"); } transfer(); return stackB.pop(); } public Integer peek(){ if(stackA.isEmpty()&&stackB.isEmpty()){ throw new RuntimeException("Queue is empty!"); } transfer(); return stackB.peek(); }}边栏推荐
- How to solve the data inconsistency between redis and MySQL?
- 黑苹果安装教程OC引导「建议收藏」
- Visual design tutorial of word cloud
- RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
- Notes on the use of official jeecg components (under update...)
- CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
- Embedded design and development project - liquid level detection and alarm system
- Special test for cold and hot start of app
- How to open an account on the flush? Is it safe
- 2022年中国运维安全产品市场规模及发展趋势预测分析
猜你喜欢

How to solve the problem that the computer wireless network does not display the network list

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

欧拉恒等式:数学史上的真正完美公式!

Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练

接雨水系列问题

Luogu_ P1303 A*B Problem_ High precision calculation

排序

China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share

Visual design tutorial of word cloud

iNFTnews | 科技巨头加快进军Web3和元宇宙
随机推荐
If a programmer goes to prison, will he be assigned to write code?
Mobile web training day-2
Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects
木兰开放作品许可证1.0面向社会公开征求意见
How about stock online account opening and account opening process? Is it safe to open a mobile account?
再谈exception——异常抛出时会发生什么?
几百行代码实现一个 JSON 解析器
China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
真香啊!最全的 Pycharm 常用快捷键大全!
iNFTnews | 科技巨头加快进军Web3和元宇宙
Solution to directory access of thinkphp6 multi-level controller
接雨水系列问题
Regular matching numbers, English and English symbols
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
其他国产手机未能填补华为的空缺,苹果在高端手机市场已无对手
Go array and slice, []byte to string[easy to understand]
为什么新的5G标准将为技术栈带来更低的 TCO
Embedded design and development project - liquid level detection and alarm system
MySQL从库Error:“You cannot ‘Alter‘ a log table...“
排序
