当前位置:网站首页>[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();
}
}
}
边栏推荐
- MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
- Symbolic representation of functions in deep learning papers
- open-mmlab labelImg mmdetection
- Types de variables JS et transformations de type communes
- Working principle of genius telephone watch Z3
- [Offer29] 排序的循环链表
- 程序员老鸟都会搞错的问题 C语言基础 指针和数组
- MySQL时间、时区、自动填充0的问题
- Learning notes of JS variable scope and function
- ESP learning problem record
猜你喜欢
Redis based distributed ID generator
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Pytorch: tensor operation (I) contiguous
ES6语法总结--上篇(基础篇)
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
基于Redis的分布式ID生成器
C language callback function [C language]
CUDA C programming authoritative guide Grossman Chapter 4 global memory
JS變量類型以及常用類型轉換
随机推荐
History object
ES6语法总结--下篇(进阶篇 ES6~ES11)
Pytoch implements simple linear regression demo
Symbolic representation of functions in deep learning papers
[Offer29] 排序的循环链表
Types de variables JS et transformations de type communes
【ESP32学习-1】Arduino ESP32开发环境搭建
Arduino uno R3 register writing method (1) -- pin level state change
NRF24L01 troubleshooting
Kconfig Kbuild
AMBA、AHB、APB、AXI的理解
Classification, understanding and application of common methods of JS array
The first simple case of GNN: Cora classification
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
MySQL time, time zone, auto fill 0
Amba, ahb, APB, Axi Understanding
[esp32 learning-1] construction of Arduino esp32 development environment
MySQL replacement field part content
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
关于Gateway中使用@Controller的问题