当前位置:网站首页>[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();
}
}
}
边栏推荐
- NRF24L01 troubleshooting
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- Kaggle competition two Sigma connect: rental listing inquiries
- [Leetcode15]三数之和
- Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
- Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
- Flink late data processing (3)
- Single chip Bluetooth wireless burning
- Gateway fails to route according to the service name, and reports an error service unavailable, status=503
- What is the maximum length of MySQL varchar field
猜你喜欢
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
Single chip Bluetooth wireless burning
程序设计大作业:教务管理系统(C语言)
基于Redis的分布式锁 以及 超详细的改进思路
单片机蓝牙无线烧录
MySQL时间、时区、自动填充0的问题
NRF24L01 troubleshooting
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Page performance optimization of video scene
Basic operations of databases and tables ----- classification of data
随机推荐
PT OSC deadlock analysis
Vscode basic configuration
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Important methods of array and string
ARM PC=PC+8 最便于理解的阐述
Minio文件下载问题——inputstream:closed
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
ESP learning problem record
Types de variables JS et transformations de type communes
The dolphin scheduler remotely executes shell scripts through the expect command
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
. elf . map . list . Hex file
AMBA、AHB、APB、AXI的理解
[offer78]合并多个有序链表
JS数组常用方法的分类、理解和运用
Cannot change version of project facet Dynamic Web Module to 2.3.
Kconfig Kbuild
[esp32 learning-2] esp32 address mapping
Classification, understanding and application of common methods of JS array
程序员老鸟都会搞错的问题 C语言基础 指针和数组