当前位置:网站首页>LeetCode刷题笔记:622.设计循环队列
LeetCode刷题笔记:622.设计循环队列
2022-08-03 11:30:00 【LeBron Le】
1. 问题描述
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
- MyCircularQueue(k): 构造器,设置队列长度为 k 。
- Front: 从队首获取元素。如果队列为空,返回 -1 。
- Rear: 获取队尾元素。如果队列为空,返回 -1 。
- enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
- deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
- isEmpty(): 检查循环队列是否为空。
- isFull(): 检查循环队列是否已满。
2. 解题思路
采用链表实现循环队列
定义循环队列的属性:
① head 为链表的头节点,队列的头节点。
② tail 为链表的尾节点,队列的尾节点。
③ capacity 为队列的容量,即队列可以存储的最大元素数量。
④ size 为队列当前元素的数量。
3. 代码实现
class MyCircularQueue {
private ListNode head;
private ListNode tail;
private int capacity;
private int size;
public MyCircularQueue(int k) {
capacity = k;
size = 0;
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
ListNode node = new ListNode(value);
if (head == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
size++;
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
head = head.next;
size--;
return true;
}
public int Front() {
if (isEmpty()) {
return -1;
}
return head.val;
}
public int Rear() {
if (isEmpty()) {
return -1;
}
return tail.val;
}
public boolean isEmpty() {
if (size == 0) {
return true;
}
return false;
}
public boolean isFull() {
if (size == capacity) {
return true;
}
return false;
}
}
/** * 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(); */
- 时间复杂度:初始化和每项操作的时间复杂度均为O(1)。
- 空间复杂度:O(k),其中 k 为给定的队列元素数目。
边栏推荐
- 缓存--伪共享问题
- 码率vs.分辨率,哪一个更重要?
- Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
- Generate interface documentation online
- Activiti产生的背景和作用
- Fastjson反序列化
- 使用.NET简单实现一个Redis的高性能克隆版(一)
- asdn涨薪技术之apifox+Jenkins如何玩转接口自动化测试
- Machine Learning (Chapter 1) - Feature Engineering
- 性能优化|从ping延时看CPU电源管理
猜你喜欢
请问应该用什么关键字将内容主题设置为 dark 呢

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

MySQL database combat (1)
![[Detailed explanation of binary search plus recursive writing method] with all the code](/img/51/c4960575a59f8ca7f161b310e47b27.png)
[Detailed explanation of binary search plus recursive writing method] with all the code

Binary search tree (search binary tree) simulation implementation (there is a recursive version)

Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million

干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换

性能优化|从ping延时看CPU电源管理

【二分查找详解外加递归写法】附有全部代码

LyScript implements memory stack scanning
随机推荐
【Star项目】小帽飞机大战(九)
FR9811S6 SOT-23-6 23V, 2A Synchronous Step-Down DC/DC Converter
劝退背后。
用于发票处理的 DocuWare,摆脱纸张和数据输入的束缚,自动处理所有收到的发票
二叉搜索树(搜索二叉树)模拟实现(有递归版本)
JS快速高效开发技巧指南(持续更新)
3分钟实现内网穿透(基于ngrok实现)
直播弱网优化
build --repot
GET 和 POST 有什么区别?
MySQL - 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded
谷歌研究员被群嘲:研究员爆料AI有意识,被勒令休假
opencv学习—VideoCapture 类基础知识「建议收藏」
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
码率vs.分辨率,哪一个更重要?
"Global Digital Economy Conference" landed in N World, Rongyun provides communication cloud service support
Traceback (most recent call last): File
Redis发布订阅和数据类型
shell编程-测试
使用.NET简单实现一个Redis的高性能克隆版(一)