当前位置:网站首页>[leetcode622]设计循环队列
[leetcode622]设计循环队列
2022-07-06 09:17:00 【劲腰傩舞】
虽然但是,基础的东西也写写
题目
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-circular-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-circular-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class MyCircularQueue {
int[] queue;
/*分别是头指针和尾指针*/
int front,rear;
int size;
public MyCircularQueue(int k) {
/*创建一个数组。要能够区分空队列和满队列的情况。需要多保留一位*/
/*头指针和尾指针重叠的时候表示空队列*/
/*尾指针指向队列最后一个节点的下一个位置*/
/*当尾指针的下一个位置是头指针的时候,队列满*/
/*所以会浪费一个位置*/
queue=new int[k+1];
front=rear=0;
/*队列的实际长度,用来计算头指针和尾指针移动后的位置*/
size=k+1;
}
public boolean isEmpty() {
return rear==front;
}
public boolean isFull() {
/*队列满的时候,由于是循环队列,尾指针可能在头指针的前面或者后边,所以需要取余操作*/
/*取余,考虑的是rear==size-1的情况*/
return (rear+1)%size==front;
}
public boolean enQueue(int value) {
if(isFull())
return false;
else{
queue[rear]=value;
rear=(rear+1)%size;
return true;
}
}
public boolean deQueue() {
if(isEmpty())
return false;
else {
front=(front+1)%size;
return true;
}
}
public int Front() {
if(isEmpty())
return -1;
else return queue[front];
}
public int Rear() {
if(isEmpty())
return -1;
/*括号中还需要+size,考虑的是rear==0的情况*/
else return queue[(rear-1+size)%size];
}
}
边栏推荐
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- MySQL time, time zone, auto fill 0
- VSCode基础配置
- ES6语法总结--上篇(基础篇)
- 高通&MTK&麒麟 手機平臺USB3.0方案對比
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
- Inline detailed explanation [C language]
- OSPF message details - LSA overview
猜你喜欢
Basic operations of databases and tables ----- classification of data
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
[esp32 learning-2] esp32 address mapping
JS variable types and common type conversions
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
RT thread API reference manual
STM32 how to locate the code segment that causes hard fault
Kconfig Kbuild
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
随机推荐
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Characteristics, task status and startup of UCOS III
JS正则表达式基础知识学习
Basic operations of databases and tables ----- creating data tables
GCC compilation options
open-mmlab labelImg mmdetection
Walk into WPF's drawing Bing Dwen Dwen
Basic operations of databases and tables ----- view data tables
OSPF message details - LSA overview
Symbolic representation of functions in deep learning papers
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
高通&MTK&麒麟 手機平臺USB3.0方案對比
ESP learning problem record
Whistle+switchyomega configure web proxy
Redis based distributed ID generator
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
MySQL時間、時區、自動填充0的問題
高通&MTK&麒麟 手机平台USB3.0方案对比
Use of lists
Time slice polling scheduling of RT thread threads