当前位置:网站首页>[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();
}
}
}
边栏推荐
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- 数据库课程设计:高校教务管理系统(含代码)
- MySQL takes up too much memory solution
- 嵌入式启动流程
- Pytorch: tensor operation (I) contiguous
- CUDA C programming authoritative guide Grossman Chapter 4 global memory
- map文件粗略分析
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- HCIP Day 12
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
猜你喜欢

JS regular expression basic knowledge learning

The dolphin scheduler remotely executes shell scripts through the expect command

Arduino uno R3 register writing method (1) -- pin level state change

NRF24L01故障排查

Kaggle competition two Sigma connect: rental listing inquiries (xgboost)

Redis 缓存更新策略,缓存穿透、雪崩、击穿问题

Symbolic representation of functions in deep learning papers

ESP8266连接onenet(旧版MQTT方式)

单片机蓝牙无线烧录

Working principle of genius telephone watch Z3
随机推荐
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
JS数组常用方法的分类、理解和运用
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Vulnhub target: hacknos_ PLAYER V1.1
Inline detailed explanation [C language]
gcc 编译选项
NRF24L01故障排查
嵌入式启动流程
ES6语法总结--上篇(基础篇)
C language callback function [C language]
Who says that PT online schema change does not lock the table, or deadlock
Redis based distributed ID generator
VIM command line notes
History object
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Basic operations of databases and tables ----- classification of data
Important methods of array and string
Vscode basic configuration
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect