当前位置:网站首页>[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();
}
}
}
边栏推荐
猜你喜欢
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Learning notes of JS variable scope and function
基于Redis的分布式锁 以及 超详细的改进思路
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
基于Redis的分布式ID生成器
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Working principle of genius telephone watch Z3
Common properties of location
open-mmlab labelImg mmdetection
随机推荐
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
[Leetcode15]三数之和
Mysqldump error1066 error solution
Servlet
Symbolic representation of functions in deep learning papers
Dead loop in FreeRTOS task function
Générateur d'identification distribué basé sur redis
Redis cache update strategy, cache penetration, avalanche, breakdown problems
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
基于Redis的分布式锁 以及 超详细的改进思路
MySQL takes up too much memory solution
C language callback function [C language]
map文件粗略分析
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Esp8266 connect onenet (old mqtt mode)
HCIP Day 12
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
JS变量类型以及常用类型转换
Pat 1097 duplication on a linked list (25 points)