当前位置:网站首页>由两个栈组成的队列
由两个栈组成的队列
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(); }}边栏推荐
- Latest summary! 30 provinces announce 2022 college entrance examination scores
- To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
- Inftnews | technology giants accelerate their march into Web3 and metauniverse
- 你的代码会说话吗?(上)
- Special test for cold and hot start of app
- PCB understand Wang, are you? I am not
- NFT digital collection system development (3D modeling economic model development case)
- 《蛤蟆先生去看心里医生》阅读笔记
- How to solve the data inconsistency between redis and MySQL?
- 原生JS 实现页面元素的拖动 拖拽
猜你喜欢

StackOverflow 2022数据库年度调查

RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)

Why do more and more users give up swagger and choose apifox

If a programmer goes to prison, will he be assigned to write code?

Pytorch model

Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain

Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"

PostgreSQL surpasses MySQL

Stackoverflow 2022 database annual survey

BERT为何无法彻底干掉BM25??
随机推荐
Hematemesis recommends 17 "wheels" to improve development efficiency
2021计算机三级数据库大题总结
Pytorch model parameter adjustment and training related contents
中国数据库技术大会(DTCC)特邀科蓝SUNDB数据库专家精彩分享
To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
PHP obtains the beginning and end time of the month according to the month and year
抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫
How to set auto format after saving code in vscade
Why will the new 5g standard bring lower TCO to the technology stack
Pytorch main modules
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
Data analysis - promoter evolution analysis
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
My hematemesis collection integrates script teaching from various classic shell books. As Xiaobai, come quickly
Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
2.01 backpack problem
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
G : 最大流问题
Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
