当前位置:网站首页>[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();
}
}
}
边栏推荐
- Selective sorting and bubble sorting [C language]
- STM32 how to locate the code segment that causes hard fault
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- Several declarations about pointers [C language]
- arduino JSON数据信息解析
- Programmers can make mistakes. Basic pointers and arrays of C language
- Oppo vooc fast charging circuit and protocol
- AMBA、AHB、APB、AXI的理解
- E-commerce data analysis -- salary prediction (linear regression)
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
猜你喜欢
JS regular expression basic knowledge learning
open-mmlab labelImg mmdetection
Reno7 60W super flash charging architecture
ES6语法总结--下篇(进阶篇 ES6~ES11)
NRF24L01故障排查
Cannot change version of project facet Dynamic Web Module to 2.3.
Pytorch four commonly used optimizer tests
RT thread API reference manual
Générateur d'identification distribué basé sur redis
Basic knowledge of lithium battery
随机推荐
Detailed explanation of Union [C language]
Characteristics, task status and startup of UCOS III
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
ES6语法总结--下篇(进阶篇 ES6~ES11)
JS正则表达式基础知识学习
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
map文件粗略分析
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Learning notes of JS variable scope and function
E-commerce data analysis -- salary prediction (linear regression)
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Minio文件下载问题——inputstream:closed
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
关键字 inline (内联函数)用法解析【C语言】
Arduino gets the length of the array
Programmers can make mistakes. Basic pointers and arrays of C language
Walk into WPF's drawing Bing Dwen Dwen
MySQL replacement field part content