当前位置:网站首页>力扣:用两个栈实现一个队列
力扣:用两个栈实现一个队列
2022-06-22 03:29:00 【想飞的yu】
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue(){
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(stack2.isEmpty()){
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
if(stack2.isEmpty()){
return -1;
}else {
return stack2.pop();
}
}
}
这个题很明确用两个栈实现一个队列,差不多实现方法也给出来了。
栈的特性是先进后出,队列的特性是先进先出。
很明确的,只要经过两次的入栈,即先进后出,先进后出,自然得到的结果就是先进先出,和队列的结果相同了。
其他的就是一些逻辑判断的问题。
边栏推荐
- Shelling of ESP law of reverse crackme
- 倍福TwinCAT3中PLC程序变量定义和硬件IO关联
- The cloned VMware virtual host network card cannot be started solution
- Splunk: Auto load Balanced TCP Output issue
- [网鼎杯 2018]Fakebook1 参考
- C51的一些日记
- c# 自定义排序
- 克隆的vmware虚拟主机网卡无法启动解决办法
- Decorator II property - short answer logic
- 基于Pytorch的图像分类总结:Swin Transformer
猜你喜欢

Understand virtual memory from the root

利用jemalloc解决flink的内存溢出问题

R data analysis: significance and practice of calibration curve and DCA curve in clinical prediction model

【Leetcode】17回溯(电话号码的字母组合)

How to break through the sales dilemma of clothing stores

618来袭,当贝X3和极米H3S怎么选?带你全方位深度解析

简单入门到彻底搞懂防抖和节流

EU5, eu7, EX3, Ex5 install third-party apps
![[nvme2.0b 12] NVM capacity model](/img/e5/c898cb1eeeabce757a2b07b08f9219.png)
[nvme2.0b 12] NVM capacity model

php使用composer
随机推荐
Template as interface
Nebula Graph学习篇2_版本v2.6.1之前的bug导致OOM
Usage of tail
vim常用命令
How to synchronize the oak camera?
达梦数据库客户端屏蔽sql关键字
作为接口的模板
为什么在高并发下很容易就被setInterval给坑了
倍福TwinCAT3控制器和控制器间的Ads通讯
mysql-索引创建、优化分析、索引优化
Based on logback XML to realize the insensitive operation of saving log information
利用jemalloc解决flink的内存溢出问题
OAK相机如何实现同步?
golang标准库time
How to break through the sales dilemma of clothing stores
Analyzing iceberg merge tasks to resolve data conflicts
基于logback.xml实现保存日志信息的无感操作
The cloned VMware virtual host network card cannot be started solution
【NVMe2.0b 5】NVM Subsystem
Deep learning final review