当前位置:网站首页>[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();
}
}
}
边栏推荐
猜你喜欢

ES6 grammar summary -- Part I (basic)

level16

Mysql database interview questions

Basic operations of databases and tables ----- view data tables

单片机蓝牙无线烧录

Understanding of AMBA, AHB, APB and Axi

Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0

(五)R语言入门生物信息学——ORF和序列分析

Programmers can make mistakes. Basic pointers and arrays of C language

Redis based distributed locks and ultra detailed improvement ideas
随机推荐
VIM command line notes
Vscode basic configuration
高通&MTK&麒麟 手機平臺USB3.0方案對比
AMBA、AHB、APB、AXI的理解
Cannot change version of project facet Dynamic Web Module to 2.3.
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
[esp32 learning-2] esp32 address mapping
Inline detailed explanation [C language]
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Characteristics, task status and startup of UCOS III
.elf .map .list .hex文件
Unit test - unittest framework
程序员老鸟都会搞错的问题 C语言基础 指针和数组
1081 rational sum (20 points) points add up to total points
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Detailed explanation of Union [C language]
Arduino uno R3 register writing method (1) -- pin level state change
列表的使用