当前位置:网站首页>2022.08.02_每日一题
2022.08.02_每日一题
2022-08-05 11:48:00 【诺.い】
622. 设计循环队列
题目描述
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。Front: 从队首获取元素。如果队列为空,返回 -1 。Rear: 获取队尾元素。如果队列为空,返回 -1 。enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。isEmpty(): 检查循环队列是否为空。isFull(): 检查循环队列是否已满。
示例:
MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1); // 返回 true
circularQueue.enQueue(2); // 返回 true
circularQueue.enQueue(3); // 返回 true
circularQueue.enQueue(4); // 返回 false,队列已满
circularQueue.Rear(); // 返回 3
circularQueue.isFull(); // 返回 true
circularQueue.deQueue(); // 返回 true
circularQueue.enQueue(4); // 返回 true
circularQueue.Rear(); // 返回 4
提示:
- 所有的值都在 0 至 1000 的范围内;
- 操作数将在 1 至 1000 的范围内;
- 请不要使用内置的队列库。
class MyCircularQueue {
int size;
int[] arr;
// 尾指针默认指向结尾元素的下一个索引
int rear;
int front;
public MyCircularQueue(int size) {
this.size = size + 1;
this.arr = new int[size + 1];
this.rear = 0;
this.front = 0;
}
// add
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
arr[rear] = value;
rear = (rear + 1) % size;
return true;
}
// del
public boolean deQueue() {
if (isEmpty()) {
return false;
}
front = (front + 1) % size;
return true;
}
// getFront
public int Front() {
return isEmpty() ? -1 : arr[front];
}
// getRear
public int Rear() {
return isEmpty() ? -1 : arr[(rear - 1 + size) % size];
}
public boolean isEmpty() {
return rear == front;
}
public boolean isFull() {
return ((rear + 1) % size) == 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(); */
边栏推荐
- knife4j
- ORACLE ASM提供的三种冗余方式
- Keras 分割网络自定义评估函数 - mean iou
- 再获殊荣 | 赛宁网安入选2022年度“培育独角兽”企业榜单
- WPF开发随笔收录-WriteableBitmap绘制高性能曲线图
- 小红的aba子序列(离散化、二分、dp维护区间最短)
- 163_Tricks_Power BI one-click batch creation of custom field parameters
- 不是吧?还有人不会定位线上MySQL慢查询问题?
- Official release 2022 Nanjing Zhibo Expo is scheduled to be held in Xinzhuang National Exhibition in October
- 冬日里,28℃的爱情
猜你喜欢

LeetCode刷题(8)

Four, kubeadm single master

没开发人员,接到开发物联网系统的活儿,干不干?

【硬件架构的艺术】学习笔记(2)同步和复位

623. Add a row to a binary tree: Simple binary tree traversal problems

互联网行业凛冬之至,BATM的程序员是如何应对中年危机的?

365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree

【着色器实现Flicker“DJ”闪烁效果_Shader效果第十五篇】

解决2022Visual Studio中scanf返回值被忽略问题

Cesium.js 三维土壤地质剖面分割挖掘
随机推荐
swig 语法介绍
Android development with Kotlin programming language three loop control
PMP每日一练 | 考试不迷路-8.5(包含敏捷+多选)
小红的aba子序列(离散化、二分、dp维护区间最短)
Cesium.js 地形挖洞
Web3 中的安全问题和防范
#yyds干货盘点#JS数组和树相互转化
nyoj757 期末考试 (优先队列)
PHP高级检索功能的实现以及动态拼接SQL
Introduction to the Evolution of Data Governance System
基于NSQ搭建高可用分布式消息队列
Gray value and thermal imaging understanding
Go compilation principle series 9 (function inlining)
Go Quick Start Guide: Basic Types
版本控制篇 | 龙智邀您共赴GOPS全球运维大会,探索大规模、敏捷、高质量、开放式的软件研发与运营之路
Android development with Kotlin programming language II Conditional control
Letter from Silicon Valley: Act fast, Facebook, Quora and other successful "artifacts"!
5G NR system messages
2022年6月互联网医疗领域月度观察
碘乙酰胺在Desthiobiotin-Iodoacetamide试剂中的作用?