当前位置:网站首页>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();
*/
边栏推荐
- MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
- MySQL详细学习教程(建议收藏)
- Install porterLB
- 力扣刷题之合并两个有序数组
- Compose原理-compose中是如何实现事件分法的
- ScrollView嵌套RV,滑动有阻力不顺滑怎么办?
- ImportError: /lib/libgdal.so.26: undefined symbol: sqlite3_column_table_name
- flex布局
- OSError: [WinError 123] 文件名、目录名或卷标语法不正确
- 基于DMS的数仓智能运维服务,知多少?
猜你喜欢
随机推荐
Web项目Controller统一返回实体类
技术开发人员常用的安全浏览器
云图说丨初识华为云微服务引擎CSE
红日安全内网渗透靶场-VulnStack-1
MVC vs MVP
How does MySQL permanently support Chinese input once and for all?
2022年7月国产数据库大事记
SQL代码需要供其他人复用,为什么传统的复制代码不可靠?
【C语言学习笔记(六)】分支与跳转(if、else、continue、break、switch)
要想成为黑客,离不开这十大基础知识
实时渲染器不止lumion,Chaos Vantage你值得一试
MySQL如何一劳永逸的永久支持输入中文
WEB 渗透之RCE
PHP base notes - NO. 1
ctfshow php features
YAML中多行字符串的配置方法:|+、 |、 |-、 >+、 >、 >-的区别
懵逼!阿里一面被虐了,幸获内推华为技术四面,成功拿到offer,年薪40w
PreFixSum前缀和
阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
go语言实现导出string字符串到文件中