当前位置:网站首页>[offer9] implement queues with two stacks
[offer9] implement queues with two stacks
2022-07-06 12:25:00 【Vigorous waist Nuo dance】
subject
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 )
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]
// explain : Entering . The brackets above are operations . The following brackets are the data of the operation .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class CQueue {
private Stack<Integer> in;// Specially for joining the team
private Stack<Integer> out;// Out of the team
public CQueue() {
in=new Stack<>();
out=new Stack<>();
}
public void appendTail(int value) {
in.push(value);
}
public int deleteHead() {
if(!out.empty()){
return out.pop();
}else{
while(!in.empty()){
out.push(in.pop());
}
return out.isEmpty()?-1:out.pop();
}
}
}
边栏推荐
- SSD technical features
- Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
- [Leetcode15]三数之和
- MySQL time, time zone, auto fill 0
- Kconfig Kbuild
- Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
- [esp32 learning-2] esp32 address mapping
- Talking about the startup of Oracle Database
- 关于Gateway中使用@Controller的问题
- Pytoch temperature prediction
猜你喜欢

Classification, understanding and application of common methods of JS array

程序设计大作业:教务管理系统(C语言)

dosbox第一次使用

Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0

Single chip Bluetooth wireless burning

Pytorch four commonly used optimizer tests

Time slice polling scheduling of RT thread threads

ES6 grammar summary -- Part 2 (advanced part es6~es11)

E-commerce data analysis -- salary prediction (linear regression)

Générateur d'identification distribué basé sur redis
随机推荐
JS variable types and common type conversions
js 变量作用域和函数的学习笔记
Imgcat usage experience
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
Inline detailed explanation [C language]
ESP learning problem record
CUDA C programming authoritative guide Grossman Chapter 4 global memory
Walk into WPF's drawing Bing Dwen Dwen
Priority inversion and deadlock
SSD technical features
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
AMBA、AHB、APB、AXI的理解
Redis cache update strategy, cache penetration, avalanche, breakdown problems
JS 函数提升和var变量的声明提升
Générateur d'identification distribué basé sur redis
imgcat使用心得
Detailed explanation of truncate usage
Pat 1097 duplication on a linked list (25 points)
Redis based distributed locks and ultra detailed improvement ideas