当前位置:网站首页>通过两个stack来实现Queue
通过两个stack来实现Queue
2022-06-26 22:54:00 【生产队的驴儿】
题目
As the title described, you should only use two stacks to implement a queue’s actions.
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element.
正如标题所述,你只能使用两个栈来实现队列的一些操作。队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素。pop和top方法都应该返回第一个元素的值。
题目解析
Stack是先进后出 First in Last Out
Queue 是先进先出 First in First Out
通过两个stack来回颠倒可以实现Queue结构
思路
两个stack
push入队 时候,即 添加元素,直接放入 stack1
pop时候,从stack2开始往出弹,如果stack2为空,则执行stack1全部放入stack2
然后,再从stack2往出弹。
top时候,直接执行stack。peek() 查看stack2的顶端元素就可以了
如果stack2为空,就把stack1里面全部放入stack2,然后从stack2取peek
总结
入队时,将元素压入s1。
出队时,判断s2是否为空,如不为空,则直接弹出顶元素;
如为空,则将s1的元素逐个“倒入”s2,把最后一个元素弹出并出队。
代码
public class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
// do intialization if necessary
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/* * @param element: An integer * @return: nothing */
public void push(int element) {
// write your code here
stack1.push(element);
}
/* * @return: An integer */
public int pop() {
// write your code here
if(!stack2.isEmpty()){
return stack2.pop();
}
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
/* * @return: An integer */
public int top() {
// write your code here
if(!stack2.isEmpty()){
return stack2.peek();
}
while( !stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.peek();
}
}
Reference
https://www.lintcode.com/problem/40/solution/18895
https://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html
边栏推荐
- 尚硅谷DolphinScheduler视频教程发布
- Restfultoolkitx of idea utility plug-in -- restful interface debugging
- 电子协会 C语言 1级 30 、 等差数列末项计算
- What are the test case design methods?
- Bs-gx-016 implementation of textbook management system based on SSM
- 分享三種在Excel錶格中自動求和的方法
- Design of master-slave replication system
- Different subsequence problems I
- Which platform is the safest for buying stocks and opening accounts? Ask for sharing
- leetcode:152. Product maximum subarray [consider DP of two dimensions]
猜你喜欢

Vulnhub's dc9

VB. Net class library (Advanced - 2 overload)

CVPR 2022 - Interpretation of selected papers of meituan technical team

树莓派初步使用

300题 第三讲 向量组
![[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process](/img/42/c2a25bb7a9fdad8fe0a048e9af44ca.jpg)
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process

Weaving dream collection plug-ins are recommended to be free collection plug-ins
![[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]](/img/8a/f5847eef7318f3db54aa2d4405b06a.jpg)
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]

Yolov6: the fast and accurate target detection framework is open source

L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
随机推荐
Data governance does everything
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
Common configuration of jupyterlab
数据清洗工具flashtext,效率直接提升了几十倍数
Is it safe to open an account and buy stocks? Who knows
树莓派初步使用
【混合编程jni 】第九篇之Jni总结
leetcode - 买卖股票的最佳时机
【710. 黑名单中的随机数】
FPGA -vga display
不同的子序列问题I
Why don't I recommend going to sap training institution for training?
Unity3d plug-in anyportrait 2D bone animation
6.24 learning content
npm 命令提示Error: EACCES: permission denied
简述unity的模型动画功能
【老卫搞机】090期:键盘?主机?全功能键盘主机!
Flower shop window layout [dynamic planning]