当前位置:网站首页>通过两个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
边栏推荐
- leetcode:710. Random numbers in the blacklist [mapping thinking]
- 为什么我不推荐去SAP培训机构参加培训?
- WordPress collection plug-ins are recommended to be free collection plug-ins
- 在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档
- Unityeditor Editor Extension - table function
- [mixed programming JNI] Part 7: JNI command lines
- Pass note 【 dynamic planning 】
- Word chess based on heuristic search
- Centos7 compiling and installing redis
- 开放世界机甲游戏-Phantom Galaxies
猜你喜欢

【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】

Smartbi gives you a piece to play with Boston matrix

300题 第三讲 向量组

WordPress collection plug-ins are recommended to be free collection plug-ins

Wechat applet automatically generates punch in Poster

【界面】pyqt5和Swin Transformer对人脸进行识别

FPGA -vga display
![leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】](/img/a4/c5c31de7a0a3b34a188bfec0b5d184.png)
leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】

L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
![[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]](/img/e8/6342f2dc6e7f06a847852ce4b40719.jpg)
[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
随机推荐
运筹说 第66期|贝尔曼也有“演讲恐惧症”?
leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
Unity3d plug-in anyportrait 2D bone animation
Why don't I recommend going to sap training institution for training?
不花一分钱做个在线的gif合成服务
尚硅谷DolphinScheduler视频教程发布
电子协会 C语言 1级 30 、 等差数列末项计算
What is the “ How to remove a custom form list?
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
Parsing complex JSON in fluent
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Unity布料系統_Cloth組件(包含動態調用相關)
Three solutions for improving embedded software development environment
vulnhub之DC9
Unity布料系统_Cloth组件(包含动态调用相关)
【BUG反馈】WebIM在线聊天系统发消息时间问题
Unity3D插件 AnyPortrait 2D骨骼動畫制作
分享三种在Excel表格中自动求和的方法