当前位置:网站首页>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(); */
}
}
边栏推荐
- [depth first search] 695 Maximum area of the island
- [to be continued] [depth first search] 547 Number of provinces
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- ssh免密登录设置及使用脚本进行ssh登录并执行指令
- Developing desktop applications with electron
- [转]MySQL操作实战(一):关键字 & 函数
- [to be continued] [UE4 notes] L3 import resources and project migration
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- 2022年上半年国家教师资格证考试
猜你喜欢

Double pointer Foundation
![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)
[to be continued] [UE4 notes] L1 create and configure items

剑指 Offer 06.从头到尾打印链表

Binary search basis

Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail

Solution to the palindrome string (Luogu p5041 haoi2009)

剑指 Offer 53 - II. 0~n-1中缺失的数字

游戏商城毕业设计

SAP method of modifying system table data

Reader writer model
随机推荐
Introduction to tools in TF-A
[binary search] 69 Square root of X
每日一题-无重复字符的最长子串
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Add level control and logger level control of Solon logging plug-in
Acwing 4301. Truncated sequence
kubeadm系列-00-overview
Introduction to memory layout of FVP and Juno platforms
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
26、 File system API (device sharing between applications; directory and file API)
kubeadm系列-01-preflight究竟有多少check
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
2022上半年全国教师资格证下
利用HashMap实现简单缓存
一个新的微型ORM开源框架
High precision subtraction
Haut OJ 1401: praise energy
Acwing 4300. Two operations
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
YOLOv5添加注意力机制