当前位置:网站首页>Implement the queue through two stacks
Implement the queue through two stacks
2022-06-26 23:14:00 【Donkey of the production team】
subject
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.
As the title says , You can only use two stacks to implement some operations of the queue . Queues should support push(element),pop() and top(), among pop Is the first in the pop-up queue ( The front of ) Elements .pop and top Methods should return the value of the first element .
title
Stack First in, then out First in Last Out
Queue First in, first out First in First Out
Through two stack Reverse can be achieved Queue structure 
Ideas
Two stack
push The team When , namely Additive elements , Put it directly in stack1
pop When , from stack2 Start shooting , If stack2 It's empty , execute stack1 Put it all in stack2
then , Again from stack2 Eject .
top When , Direct execution stack.peek() see stack2 The top element of the
If stack2 It's empty , Just put stack1 Put it all in stack2, And then from stack2 take peek
summary
When you join the team , Press elements into s1.
When the team , Judge s2 Is it empty , If not empty , Pop up the top element directly ;
If blank , Will s1 Element by element “ Pour into ”s2, Pop the last element out of the queue .
Code
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
边栏推荐
- ubuntu上安装mysql
- Detailed explanation of nmap parameters
- Different subsequence problems I
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- Is there any risk in opening a new bond registration account? Is it safe?
- DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
- 【Kotlin】关键词suspend 线程操作的学习和async理解
- [fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
- [bug feedback] the problem of message sending time of webim online chat system
- How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
猜你喜欢

ASP.Net Core创建MVC项目上传文件(缓冲方式)

【界面】pyqt5和Swin Transformer对人脸进行识别
![How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]](/img/ec/1c324dcf38d07742a139aac2bab02e.png)
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]

浅谈分布式系统开发技术中的CAP定理

Redcap is ready to come out. It is indispensable to build a "meta universe"

简单测试轻量级表达式计算器Flee

Simple test lightweight expression calculator fly

300题 第三讲 向量组

xshell的安装、xftp的安装

vulnhub之dc8
随机推荐
Raspberry pie preliminary use
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)
Different subsequence problems I
nmap参数详解
Unity4.6版本下载
typora设置标题自动编号
不花一分钱做个在线的gif合成服务
阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境
Bs-gx-016 implementation of textbook management system based on SSM
C语言:简单计算器多次使用代码实现
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
Detailed explanation of nmap parameters
Why don't I recommend going to sap training institution for training?
【混合编程jni 】第十二篇 jnaerator
Wechat applet automatically generates punch in Poster
Design of master-slave replication system
Unity method for setting material and shader
Operations research says that in issue 66, Behrman also has "speech phobia"?
不同的子序列问题I