当前位置:网站首页>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();
*/
边栏推荐
猜你喜欢

阿里巴巴政委体系-第七章、阿里政委培育

Install porterLB

开发即时通讯到底需要什么样的技术,需要多久的时间

YAML中多行字符串的配置方法:|+、 |、 |-、 >+、 >、 >-的区别

APT级全面免杀与企业纵深防御体系的红蓝对抗

丙二醇二乙酸酯(Propylene Glycol Diacetate)

云图说丨初识华为云微服务引擎CSE

Don't look down upon the WebSocket!Long connection, stateful, two-way, full-duplex king is Fried

OneNote 教程,如何在 OneNote 中设置页面格式?

使用安全浏览器将网页保存为pdf的方法步骤
随机推荐
typescript学习笔记
PreFixSum前缀和
Selenium of reptiles
基于ck+redash构建MySQL慢日志+审计日志展示平台
软件测试技术之如何编写测试用例(3)
云图说丨初识华为云微服务引擎CSE
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
MySQL详细学习教程(建议收藏)
C#爬虫之通过Selenium获取浏览器请求响应结果
docker mysql 容器中执行mysql脚本文件并解决乱码
BinomialTree 二叉树
多线程和并发编程(四)
2022/08/02------Ugly number
Alibaba senior experts create a learning architecture from scratch, including Alibaba's internal technology stack PPT, PFD actual combat
G6尝试 学习
MySQL如何 drop 大表
力扣刷题之移动零
BinaryIndexedTrees树状数组
要想成为黑客,离不开这十大基础知识
MVC vs MVP