当前位置:网站首页>[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];
}
}
边栏推荐
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
- @The difference between Autowired and @resource
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- 基于Redis的分布式锁 以及 超详细的改进思路
- Dead loop in FreeRTOS task function
- [esp32 learning-1] construction of Arduino esp32 development environment
- @Autowired 和 @Resource 的区别
- Pytoch temperature prediction
- 1081 rational sum (20 points) points add up to total points
- Reno7 60W超级闪充充电架构
猜你喜欢
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
JS 函数提升和var变量的声明提升
ARM PC=PC+8 最便于理解的阐述
MySQL时间、时区、自动填充0的问题
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
OPPO VOOC快充电路和协议
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Kconfig Kbuild
AMBA、AHB、APB、AXI的理解
随机推荐
Pytorch: tensor operation (I) contiguous
map文件粗略分析
ESP8266连接onenet(旧版MQTT方式)
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Selective sorting and bubble sorting [C language]
OSPF message details - LSA overview
Kconfig Kbuild
open-mmlab labelImg mmdetection
Basic operations of databases and tables ----- view data tables
Learning notes of JS variable scope and function
STM32 how to locate the code segment that causes hard fault
Redis based distributed locks and ultra detailed improvement ideas
1081 rational sum (20 points) points add up to total points
Minio文件下载问题——inputstream:closed
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Mysqldump error1066 error solution
NRF24L01故障排查
vim命令行笔记
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Important methods of array and string