当前位置:网站首页>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
边栏推荐
- 分享三种在Excel表格中自动求和的方法
- NPM command prompt error: eacces: permission denied
- Bs-gx-016 implementation of textbook management system based on SSM
- Unity: the referenced script (unknown) on this behavior is missing“
- 不花一分钱做个在线的gif合成服务
- Using C to operate SQLSERVER database through SQL statement tutorial
- 简析攻防演练中蓝队的自查内容
- 分享三種在Excel錶格中自動求和的方法
- Unity布料系統_Cloth組件(包含動態調用相關)
- 有哪些劵商推荐?现在在线开户安全么?
猜你喜欢

Leetcode (763) -- dividing letter ranges

从位图到布隆过滤器,C#实现

开放世界机甲游戏-Phantom Galaxies

UnityEditor编辑器扩展-表格功能
![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]

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

Word chess based on heuristic search
![Flower shop window layout [dynamic planning]](/img/d9/6b8f9cd0f74e70b313d2571c2ded30.png)
Flower shop window layout [dynamic planning]

Product design in the extreme Internet Era

Briefly describe the model animation function of unity
随机推荐
数据清洗工具flashtext,效率直接提升了几十倍数
Simple test lightweight expression calculator fly
Operator介紹
CVPR2022-不对称分辨率图像的立体匹配
[mixed programming JNI] Part 6: operation of strings and arrays in native
DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
Microservices, an important part of cloud native architecture
Unity4.6版本下载
Design of master-slave replication system
vulnhub之dc8
FPGA -vga display
The sharp sword of API management -- eolink
[mixed programming JNI] Part 9: JNI summary
简述unity的模型动画功能
浅谈分布式系统开发技术中的CAP定理
xshell的安装、xftp的安装
leetcode - 买卖股票的最佳时机
【混合编程jni 】第六篇之native 中字符串和数组的操作
Unity animation knowledge of Art
Vulnhub's dc9