当前位置:网站首页>[offer9]用两个栈实现队列
[offer9]用两个栈实现队列
2022-07-06 09:17:00 【劲腰傩舞】
题目
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
输入:
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
输出:[null,null,3,-1]
//解释:输入中。上面的中括号为操作。下面的中括号为操作的数据。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class CQueue {
private Stack<Integer> in;//专门用来入队
private Stack<Integer> out;//出队
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();
}
}
}
边栏推荐
- Reno7 60W super flash charging architecture
- GCC compilation options
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- JS function promotion and declaration promotion of VaR variable
- Selective sorting and bubble sorting [C language]
- Walk into WPF's drawing Bing Dwen Dwen
- Basic operations of databases and tables ----- classification of data
- Use of lists
- Vscode basic configuration
- Basic operations of databases and tables ----- creating data tables
猜你喜欢

Time slice polling scheduling of RT thread threads

Characteristics, task status and startup of UCOS III

ESP8266连接onenet(旧版MQTT方式)

arduino JSON数据信息解析

ORA-02030: can only select from fixed tables/views

锂电池基础知识

Mysql database interview questions

JS正则表达式基础知识学习

(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析

VSCode基础配置
随机推荐
Who says that PT online schema change does not lock the table, or deadlock
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
js 变量作用域和函数的学习笔记
PT OSC deadlock analysis
JS variable types and common type conversions
Understanding of AMBA, AHB, APB and Axi
VIM command line notes
AMBA、AHB、APB、AXI的理解
uCOS-III 的特点、任务状态、启动
Characteristics, task status and startup of UCOS III
基於Redis的分布式ID生成器
单片机蓝牙无线烧录
JS数组常用方法的分类、理解和运用
ESP8266连接onenet(旧版MQTT方式)
Whistle+switchyomega configure web proxy
如何给Arduino项目添加音乐播放功能
JS变量类型以及常用类型转换
高通&MTK&麒麟 手机平台USB3.0方案对比
CUDA C programming authoritative guide Grossman Chapter 4 global memory
数据库课程设计:高校教务管理系统(含代码)