当前位置:网站首页>[leetcode622] design circular queue
[leetcode622] design circular queue
2022-07-06 12:25:00 【Vigorous waist Nuo dance】
Although but , Basic things are also written
subject
Design your loop queue implementation . Circular queue is a linear data structure , Its operation performance is based on FIFO( fifo ) Principle and the end of the team is connected behind the head of the team to form a loop . It's also called “ Ring buffer ”.
One of the benefits of circular queues is that we can take advantage of the previously used space in this queue . In a normal queue , Once a queue is full , We can't insert the next element , There's room even in front of the queue . But with circular queues , We can use this space to store new values .
Your implementation should support the following operations :
MyCircularQueue(k): Constructors , Set queue length to k .
Front: Get elements from team leader . If the queue is empty , return -1 .
Rear: Get team end element . If the queue is empty , return -1 .
enQueue(value): Insert an element into the loop queue . True if successfully inserted .
deQueue(): Remove an element from the loop queue . True if deleted successfully .
isEmpty(): Check if the loop queue is empty .
isFull(): Check if the loop queue is full .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/design-circular-queue
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/design-circular-queue
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class MyCircularQueue {
int[] queue;
/* They are the head pointer and the tail pointer */
int front,rear;
int size;
public MyCircularQueue(int k) {
/* Create an array . Be able to distinguish between empty queues and full queues . Need to keep one more */
/* When the head pointer and the tail pointer overlap, it indicates an empty queue */
/* The tail pointer points to the next position of the last node in the queue */
/* When the next position of the tail pointer is the head pointer , The queue is full */
/* So it will waste a place */
queue=new int[k+1];
front=rear=0;
/* The actual length of the queue , Used to calculate the position of the head pointer and the tail pointer after moving */
size=k+1;
}
public boolean isEmpty() {
return rear==front;
}
public boolean isFull() {
/* When the line is full , Because it's a circular queue , The tail pointer may be in front of or behind the head pointer , Therefore, it is necessary to take the remainder */
/* Remainder , The consideration is rear==size-1 The situation of */
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;
/* Also required in brackets +size, The consideration is rear==0 The situation of */
else return queue[(rear-1+size)%size];
}
}
边栏推荐
- Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
- Talking about the startup of Oracle Database
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
- Mysqldump error1066 error solution
- imgcat使用心得
- level16
- .elf .map .list .hex文件
- How to add music playback function to Arduino project
- [Red Treasure Book Notes simplified version] Chapter 12 BOM
- About using @controller in gateway
猜你喜欢
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Learning notes of JS variable scope and function
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Basic operations of databases and tables ----- classification of data
单片机蓝牙无线烧录
ES6语法总结--下篇(进阶篇 ES6~ES11)
JS function promotion and declaration promotion of VaR variable
NRF24L01 troubleshooting
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
level16
随机推荐
基于Redis的分布式锁 以及 超详细的改进思路
By v$rman_ backup_ job_ Oracle "bug" caused by details
Knowledge summary of request
2021.11.10汇编考试
[Offer29] 排序的循环链表
(五)R语言入门生物信息学——ORF和序列分析
Arduino get random number
JS變量類型以及常用類型轉換
Embedded startup process
Flink late data processing (3)
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
Page performance optimization of video scene
The dolphin scheduler remotely executes shell scripts through the expect command
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
. elf . map . list . Hex file
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Types de variables JS et transformations de type communes
[Leetcode15]三数之和
1081 rational sum (20 points) points add up to total points