当前位置:网站首页>Sword finger offer 09 Implementing queues with two stacks
Sword finger offer 09 Implementing queues with two stacks
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description
Problem analysis ( This question uses a sliding window +HashMap)
1. First define two stacks s1,s2,s1 The top of the stack is the tail of the team ,s2 The top of the stack as a team cast
2. To delete, first judge s2 Is it empty If it's not empty , Popping the top element of the stack is the header element If it is empty , take s1 All elements pop into the stack s2
3.s2 Empty return -1, If it is not empty, return the top of stack element
Code instance
class CQueue {
Stack<Integer> s1 = new Stack<>(); // s1 The top of the stack is the tail of the team , The newly added element is at the top of the stack
Stack<Integer> s2 = new Stack<>(); // s2 At the top of the stack is the head of the team
public CQueue() {
}
public void appendTail(int value) {
s1.push(value); // Push to stack s1
}
public int deleteHead() {
if(!s2.isEmpty()) {
// if s2 Not empty , Directly pop up the top element of the stack, that is, the team head element
return s2.pop();
}
// s2 It's empty , take s1 All elements pop up , Stack to s2
while(!s1.isEmpty()) {
s2.push(s1.pop());
}
return s2.isEmpty() ? -1 : s2.pop(); // s2 It's empty , return -1;s2 If it is not empty, return the top of stack element
}
}
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
}
}
边栏推荐
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- 远程升级怕截胡?详解FOTA安全升级
- [binary search] 34 Find the first and last positions of elements in a sorted array
- Add level control and logger level control of Solon logging plug-in
- 剑指 Offer 58 - II. 左旋转字符串
- Using HashMap to realize simple cache
- A preliminary study of sdei - see the essence through transactions
- Haut OJ 1347: addition of choice -- high progress addition
- Quick sort summary
- YOLOv5添加注意力機制
猜你喜欢
剑指 Offer 58 - II. 左旋转字符串
To the distance we have been looking for -- film review of "flying house journey"
读者写者模型
Heap sort summary
Solution to the palindrome string (Luogu p5041 haoi2009)
Fragment addition failed error lookup
2022年上半年国家教师资格证考试
sync. Interpretation of mutex source code
YOLOv5添加注意力机制
剑指 Offer 09. 用两个栈实现队列
随机推荐
Mysql database (I)
Haut OJ 1221: a tired day
Talking about JVM (frequent interview)
Add level control and logger level control of Solon logging plug-in
剑指 Offer 09. 用两个栈实现队列
[turn]: OSGi specification in simple terms
[to be continued] [depth first search] 547 Number of provinces
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
剑指 Offer 04. 二维数组中的查找
Little known skills of Task Manager
数仓项目的集群脚本
2022年上半年国家教师资格证考试
Yolov5 ajouter un mécanisme d'attention
Reverse one-way linked list of interview questions
MySQL数据库(一)
[轉]: OSGI規範 深入淺出
[转]:Apache Felix Framework配置属性
Time complexity and space complexity
Improvement of pointnet++
剑指 Offer 53 - II. 0~n-1中缺失的数字