当前位置:网站首页>LeetCode 622. 设计循环队列
LeetCode 622. 设计循环队列
2022-08-03 19:03:00 【Sasakihaise_】


【数组】用一个一维数组来实现,front指向头,rear指向尾的下一个(这样做是为了区分空和满,如果都指向同一个元素,那么front==rear可能是空也可能是绕了一圈的满)因为要存k个元素,所以要开k+1的空间。这样,判空就是(front + 1) % (k + 1) == rear,判满就是(rear + 1) % (k + 1) == front,添加元素为arr[rear] = val, rear = (rear + k + 1) % (k + 1),删除元素为front = (front + 1) % (k + 1),取队首元素就是arr[front],队尾元素为arr[
class MyCircularQueue {
int[] arr;
int k, front = 0, rear = 0;
public MyCircularQueue(int k) {
this.k = k;
arr = new int[k + 1];
}
public boolean enQueue(int value) {
if (isFull()) return false;
arr[rear] = value;
rear = (rear + 1) % (k + 1);
return true;
}
public boolean deQueue() {
if (isEmpty()) return false;
front = (front + 1) % (k + 1);
return true;
}
public int Front() {
if (isEmpty()) return -1;
return arr[front];
}
public int Rear() {
if (isEmpty()) return -1;
return arr[(rear + k) % (k + 1)];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return (rear + 1) % (k + 1) == front;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/class MyCircularQueue {
// 9:55.20
public:
int* arr;
int k, front = 0, rear = 0;
MyCircularQueue(int k) {
this -> k = k;
arr = (int*)malloc(sizeof(int) * (k + 1));
}
bool enQueue(int value) {
if (isFull()) return false;
arr[rear] = value;
rear = (rear + 1) % (k + 1);
return true;
}
bool deQueue() {
if (isEmpty()) return false;
front = (front + 1) % (k + 1);
return true;
}
int Front() {
if (isEmpty()) return -1;
return arr[front];
}
int Rear() {
if (isEmpty()) return -1;
return arr[(rear + k) % (k + 1)];
}
bool isEmpty() {
return front == rear;
}
bool isFull() {
return (rear + 1) % (k + 1) == front;
}
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
*/【手写链表】
class MyCircularQueue {
class Node {
int val;
Node next;
Node(int value) {
this.val = value;
}
}
Node front = null, rear = null;
int k, n = 0;
public MyCircularQueue(int k) {
this.k = k;
}
public boolean enQueue(int value) {
if (isFull()) return false;
n++;
Node node = new Node(value);
if (isEmpty()) {
front = node;
rear = node;
return true;
}
rear.next = node;
rear = node;
return true;
}
public boolean deQueue() {
if (isEmpty()) return false;
n--;
front = front.next;
return true;
}
public int Front() {
if (isEmpty()) return -1;
return front.val;
}
public int Rear() {
if (isEmpty()) return -1;
return rear.val;
}
public boolean isEmpty() {
return front == null;
}
public boolean isFull() {
return n == k;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/class Node {
public:
int val;
Node* next;
Node(int val) {
this -> val = val;
}
};
class MyCircularQueue {
public:
// 2:45 20
Node* front;
Node* rear;
int k = 0, n;
MyCircularQueue(int k) {
n = k;
}
bool enQueue(int value) {
if (isFull()) return false;
Node* node = new Node(value);
if (isEmpty()) {
front = node;
rear = node;
} else {
rear -> next = node;
rear = node;
}
k++;
return true;
}
bool deQueue() {
if (isEmpty()) return false;
k--;
front = front -> next;
return true;
}
int Front() {
if (isEmpty()) return -1;
return front -> val;
}
int Rear() {
if (isEmpty()) return -1;
return rear -> val;
}
bool isEmpty() {
return k == 0;
}
bool isFull() {
return k == n;
}
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
*/
边栏推荐
猜你喜欢
随机推荐
MPLS的简单应用于实验
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
Execute the mysql script file in the docker mysql container and solve the garbled characters
懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w
SQL server 实现触发器备份表数据
图像超分——Real-ESRGAN快速上手
ctfshow php特性
学弟:我适不适合转行做软件测试?
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
Shell:循环语句
unity3d-游戏物体控制方法
如何理解即时通讯开发移动网络的“弱”和“慢”
Power button brush the topic of merging two orderly array
软件测试技术之如何编写测试用例(3)
盲僧发现了华点——教你如何使用API接口获取数据
MySQL 啥时候用表锁,啥时候用行锁?这些你都应该知道吧
技术开发人员常用的安全浏览器
Rust:多线程并发编程
Selenium of reptiles









