当前位置:网站首页>[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];
}
}
边栏推荐
- Basic knowledge of lithium battery
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- ES6 grammar summary -- Part I (basic)
- The dolphin scheduler remotely executes shell scripts through the expect command
- Keyword inline (inline function) usage analysis [C language]
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
- Embedded startup process
- Arduino JSON data information parsing
- open-mmlab labelImg mmdetection
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
猜你喜欢

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

锂电池基础知识

Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
![C language callback function [C language]](/img/7b/910016123738240e24549ddea8a162.png)
C language callback function [C language]

Arm pc=pc+8 is the most understandable explanation

荣耀Magic 3Pro 充电架构分析

Redis 缓存更新策略,缓存穿透、雪崩、击穿问题

uCOS-III 的特点、任务状态、启动

VSCode基础配置

Classification, understanding and application of common methods of JS array
随机推荐
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
Mysqldump error1066 error solution
arduino JSON数据信息解析
单片机蓝牙无线烧录
JS正则表达式基础知识学习
uCOS-III 的特点、任务状态、启动
Basic operations of databases and tables ----- creating data tables
STM32 how to locate the code segment that causes hard fault
VIM command line notes
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Arduino uno R3 register writing method (1) -- pin level state change
关键字 inline (内联函数)用法解析【C语言】
Programmers can make mistakes. Basic pointers and arrays of C language
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
列表的使用
Unit test - unittest framework
基于Redis的分布式ID生成器
Vscode basic configuration
Selective sorting and bubble sorting [C language]
[Red Treasure Book Notes simplified version] Chapter 12 BOM