当前位置:网站首页>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
边栏推荐
- Do an online GIF synthesis service at no cost
- Reading graph augmentations to learn graph representations (lg2ar)
- 从位图到布隆过滤器,C#实现
- C language: a simple calculator is implemented by using code many times
- xshell的安装、xftp的安装
- How to write test cases and a brief introduction to go unit test tool testify
- Different subsequence problems I
- 【混合编程jni 】第十一篇之JNA详情
- Learun low code OA system construction platform
- 买股票在手机上开户安全吗 网上开户炒股安全吗
猜你喜欢

Product design in the extreme Internet Era

Operations research says that in issue 66, Behrman also has "speech phobia"?

Restfultoolkitx of idea utility plug-in -- restful interface debugging

Wechat applet automatically generates punch in Poster

Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,

论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》

300 questions lesson 3 vector group

Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times

Bs-gx-016 implementation of textbook management system based on SSM

DLA model (classification model + improved segmentation model) + deformable convolution
随机推荐
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
Electronic Society C language level 1 30, calculation of last term of arithmetic sequence
BS-GX-016基于SSM实现教材管理系统
L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
电子协会 C语言 1级 29 、 对齐输出
DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
简析攻防演练中蓝队的自查内容
【混合编程jni 】第十一篇之JNA详情
从位图到布隆过滤器,C#实现
300 questions lesson 3 vector group
[mixed programming JNI] Part 12 jnaerator
vulnhub之DC9
树莓派初步使用
[mixed programming JNI] Part 6: operation of strings and arrays in native
Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
【混合编程jni 】第十二篇 jnaerator
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
Which platform is the safest for buying stocks and opening accounts? Ask for sharing
Open world mecha games phantom Galaxy
Unity4.6版本下载