当前位置:网站首页>Sword finger offer09 Implementing queues with two stacks
Sword finger offer09 Implementing queues with two stacks
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer09. Queues are implemented with two stacks
Title Description
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example 1:
Input :
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
Output :[null,null,3,-1]
Example 2:
Input :
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
Tips :
1 <= values <= 10000 At most appendTail、deleteHead Conduct 10000 Secondary call
JAVA Code
Algorithm design idea
Stack features last in first out , The queue is characterized by first in, first out .
therefore , When using two stacks stackA,stackB When simulating a queue ,stackA As input stack , Stack elements one by one , Simulate queue entry .
stackB As output stack , Simulate the team .
When the simulation is out of the team , If stackB If there is data, it will be directly out of the stack , Otherwise it would be stackA Withdraw from the stack and press in one by one stackB in ,stackA The first element on the stack is stackB At the top of the stack . When this is done ,stackB Backstack , It is equivalent to the first in, first out of the queue .
Team space : When stackA and stackB It's all empty time .
Classical method
class CQueue {
Stack<Integer> stackA;
Stack<Integer> stackB;
public CQueue() {
// Create two new stacks ,stackA As the tail of the queue , To join the team ,stackB As the head of the queue , Used to get out of the team .
stackA= new Stack<Integer>();
stackB = new Stack<Integer>();
}
public void appendTail(int value) {
// hypothesis stack Is infinite , You don't have to judge stackA The stack is full .
stackA.push(value);
}
public int deleteHead() {
if(stackB.isEmpty()){
// No data
if(stackA.isEmpty()){
return -1;
}else{
//stackB It's empty ,stackA Isn't empty , take stackA Array stack of , Then enter the stack to stackB in , Then simulate the team operation .
while(!stackA.isEmpty()){
stackB.push(stackA.pop());
}
}
}
// Out of the team
return stackB.pop();
}
}
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */

Official methods
According to the official method , Revised as follows :
because Stack Low performance , The official has not recommended it , So we can modify the creation method of stack
Now the official recommendation is java.util.Deque. Similar to the following usage :
Deque<Integer> stackA = new ArrayDeque<Integer>();

Reference resources : There are dynamic graphic demonstrations
https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/solution/tu-jie-guan-fang-tui-jian-ti-jie-yong-li-yjbf/
expand :Deque Methods 


边栏推荐
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- [MySQL special] read lock and write lock
- Use of QT OpenGL camera
- QT OpenGL texture map
- PHP export word method (one MHT)
- MySQL time zone solution
- init. RC service failed to start
- win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
- temp
- Flutter 退出登录二次确认怎么做才更优雅?
猜你喜欢

C language improvement article (wchar_t) character type

2.8 overview of ViewModel knowledge

OpenGL draws colored triangles

QT OpenGL rotate, pan, zoom

Shardingsphere sub database and sub table < 3 >

Swagger

Shutter: overview of shutter architecture (excerpt)

Flutter 退出登录二次确认怎么做才更优雅?

Php Export word method (One MHT)
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock
随机推荐
Flutter 退出登录二次确认怎么做才更优雅?
Redis 笔记 01:入门篇
The difference between lambda and anonymous inner class
Lambda expression
SLF4J 日志门面
QT OpenGL texture map
Is it OK to open an account for online stock speculation? Is the fund safe?
Shardingsphere sub database and sub table < 3 >
Dart: self study system
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Is it safe to open an account for online stock speculation? Who can answer
ES6 standard
[embedded] - Introduction to four memory areas
Socket TCP for network communication (I)
4000 word super detailed pointer
Flutter: self study system
剑指Offer09. 用两个栈实现队列
Systemverilog-- OOP--对象的拷贝
regular expression
Flutter Widget : KeyedSubtree