当前位置:网站首页>[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();
}
}
}
边栏推荐
猜你喜欢
Analysis of charging architecture of glory magic 3pro
单片机蓝牙无线烧录
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
基於Redis的分布式ID生成器
[esp32 learning-1] construction of Arduino esp32 development environment
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
Reno7 60W super flash charging architecture
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
【ESP32学习-2】esp32地址映射
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
随机推荐
基於Redis的分布式ID生成器
ORA-02030: can only select from fixed tables/views
Amba, ahb, APB, Axi Understanding
Detailed explanation of 5g working principle (explanation & illustration)
I2C总线时序详解
Navigator object (determine browser type)
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
uCOS-III 的特点、任务状态、启动
MySQL占用内存过大解决方案
[esp32 learning-1] construction of Arduino esp32 development environment
ARM PC=PC+8 最便于理解的阐述
Analysis of charging architecture of glory magic 3pro
ES6 grammar summary -- Part I (basic)
OPPO VOOC快充电路和协议
Priority inversion and deadlock
Dead loop in FreeRTOS task function
MySQL时间、时区、自动填充0的问题
Several declarations about pointers [C language]
ESP学习问题记录