当前位置:网站首页>[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];
}
}
边栏推荐
- NRF24L01故障排查
- MySQL时间、时区、自动填充0的问题
- Arduino uno R3 register writing method (1) -- pin level state change
- How to add music playback function to Arduino project
- ES6语法总结--上篇(基础篇)
- Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
- Dead loop in FreeRTOS task function
- [leetcode622]设计循环队列
- About using @controller in gateway
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
猜你喜欢

Pytorch four commonly used optimizer tests

單片機藍牙無線燒錄

ES6 grammar summary -- Part 2 (advanced part es6~es11)

Learning notes of JS variable scope and function

Arduino JSON data information parsing

记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口

MySQL时间、时区、自动填充0的问题

Cannot change version of project facet Dynamic Web Module to 2.3.
![[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data](/img/28/221b0a51ef5f2e8ed5aeca2de8f463.jpg)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
![[esp32 learning-2] esp32 address mapping](/img/ee/c4aa0f7aed7543bb6807d7fd852c88.png)
[esp32 learning-2] esp32 address mapping
随机推荐
單片機藍牙無線燒錄
JS数组常用方法的分类、理解和运用
Expected value (EV)
[Leetcode15]三数之和
2021.11.10汇编考试
[Offer18]删除链表的节点
ARM PC=PC+8 最便于理解的阐述
[offer78]合并多个有序链表
VIM command line notes
[leetcode19]删除链表中倒数第n个结点
JS正则表达式基础知识学习
How to add music playback function to Arduino project
[offer9]用两个栈实现队列
Who says that PT online schema change does not lock the table, or deadlock
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
Basic operations of databases and tables ----- classification of data
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Classification, understanding and application of common methods of JS array
AMBA、AHB、APB、AXI的理解
JS function promotion and declaration promotion of VaR variable