当前位置:网站首页>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(); */
}
}
边栏推荐
- Maximum number of "balloons"
- Bucket sort
- YOLOv5-Shufflenetv2
- Haut OJ 1241: League activities of class XXX
- Download xftp7 and xshell7 (official website)
- Zzulioj 1673: b: clever characters???
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- kubeadm系列-02-kubelet的配置和启动
- How can the Solon framework easily obtain the response time of each request?
猜你喜欢
第六章 数据流建模—课后习题
远程升级怕截胡?详解FOTA安全升级
To be continued] [UE4 notes] L4 object editing
National teacher qualification examination in the first half of 2022
[turn to] MySQL operation practice (I): Keywords & functions
[merge array] 88 merge two ordered arrays
Reverse one-way linked list of interview questions
挂起等待锁 vs 自旋锁(两者的使用场合)
To the distance we have been looking for -- film review of "flying house journey"
[to be continued] [depth first search] 547 Number of provinces
随机推荐
Romance of programmers on Valentine's Day
Solution to the palindrome string (Luogu p5041 haoi2009)
Web APIs DOM节点
[to be continued] [UE4 notes] L3 import resources and project migration
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
利用HashMap实现简单缓存
剑指 Offer 05. 替换空格
On-off and on-off of quality system construction
[轉]: OSGI規範 深入淺出
Haut OJ 1316: sister choice buys candy III
二十六、文件系统API(设备在应用间的共享;目录和文件API)
[es practice] use the native realm security mode on es
Haut OJ 1221: a tired day
剑指 Offer 05. 替换空格
SSH password free login settings and use scripts to SSH login and execute instructions
远程升级怕截胡?详解FOTA安全升级
Introduction to tools in TF-A
Shell Sort
[allocation problem] 135 Distribute candy
Haut OJ 1350: choice sends candy