当前位置:网站首页>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 为给定的队列元素数目。
边栏推荐
猜你喜欢

【Star项目】小帽飞机大战(九)

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

Activiti产生的背景和作用

CDH6.3.2开启kerberos认证

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!...

C#/VB.NET 从PDF中提取表格

Summary of redis basics - data types (strings, lists, sets, hashes, sets)

国内数字藏品与国外NFT主要有以下六大方面的区别

Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)

Programmers architecture practice way: software architecture basic concepts and thinking
随机推荐
笔试题:金额拆分
ETL data cleaning case in MapReduce
小身材有大作用——光模块寿命分析(二)
图新地球为什么很模糊,白球、看图、下载问题深度剖析
LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
Binary search tree (search binary tree) simulation implementation (there is a recursive version)
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!...
Simple implementation of a high-performance clone of Redis using .NET (1)
浅谈SVN备份
巴比特 | 元宇宙每日必读:玩家离场,平台关停,数字藏品市场正逐渐降温,行业的未来究竟在哪里?...
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
Traceback (most recent call last): File
【Mysql】清理binlog日志的方法
LyScript implements memory stack scanning
JS快速高效开发技巧指南(持续更新)
零拷贝、MMAP、堆外内存,傻傻搞不明白...
缓存--伪共享问题
What is the ERC20 token standard?
[Detailed explanation of binary search plus recursive writing method] with all the code
程序员架构修炼之道:软件架构基本概念和思维