当前位置:网站首页>[offer9] implement queues with two stacks
[offer9] implement queues with two stacks
2022-07-06 12:25:00 【Vigorous waist Nuo dance】
subject
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]
// explain : Entering . The brackets above are operations . The following brackets are the data of the operation .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class CQueue {
private Stack<Integer> in;// Specially for joining the team
private Stack<Integer> out;// Out of the team
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();
}
}
}
边栏推荐
- 程序设计大作业:教务管理系统(C语言)
- How to add music playback function to Arduino project
- [offer78]合并多个有序链表
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- Pat 1097 duplication on a linked list (25 points)
- JS数组常用方法的分类、理解和运用
- ESP8266连接onenet(旧版MQTT方式)
- PT OSC deadlock analysis
- Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
- RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
猜你喜欢
Single chip Bluetooth wireless burning
Arduino JSON data information parsing
Pytorch four commonly used optimizer tests
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
A possible cause and solution of "stuck" main thread of RT thread
数据库课程设计:高校教务管理系统(含代码)
[esp32 learning-1] construction of Arduino esp32 development environment
ES6 grammar summary -- Part I (basic)
随机推荐
基于Redis的分布式ID生成器
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Basic operations of databases and tables ----- modifying data tables
Detailed explanation of truncate usage
ESP学习问题记录
MySQL time, time zone, auto fill 0
[offer9]用两个栈实现队列
Understanding of AMBA, AHB, APB and Axi
Inline detailed explanation [C language]
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
A possible cause and solution of "stuck" main thread of RT thread
Pytoch implements simple linear regression demo
Conditional probability
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
【ESP32学习-1】Arduino ESP32开发环境搭建
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
What is the maximum length of MySQL varchar field
(1) Introduction Guide to R language - the first step of data analysis
VSCode基础配置