当前位置:网站首页>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(); */
边栏推荐
- 安装tldr
- 手把手教你定位线上MySQL慢查询问题,包教包会
- 2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open
- Student Information Management System (first time...)
- 365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
- The importance of parameter naming, remember a JDBC parameter conflict
- Visit GOPS Long Zhi booth, Forrester's latest report: "the Forrester Wave: the fourth quarter of 2021 enterprise service management report
- 解决 json.dump 报错:TypeError - Object of type xxx is not JSON serializable
- IPMP、PMP、CPMP三个证书该如何选择,有什么区别,哪个对于工作上的
- How to use the timetable applet
猜你喜欢

365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?

分布式事务解决方案

Machine Learning - Ensemble Learning

祝所有码农七夕快乐~

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

163_技巧_Power BI 一键批量建立自定义字段参数

2022年6月互联网医疗领域月度观察

Learning Deep Compact Image Representations for Visual Tracking

163_Tricks_Power BI one-click batch creation of custom field parameters
随机推荐
nyoj757 期末考试 (优先队列)
JS 从零手写实现一个call、apply方法
祝所有码农七夕快乐~
高泽龙出席博鳌全球旅游生态大会 讲元宇宙与未来网络科技
I'm going crazy.Again A few days can not be A problem
时间格式2020-01-13T16:00:00.000Z中的T和Z分别表示什么,如何处理
深度学习(四)分析问题与调参 理论部分
163_技巧_Power BI 一键批量建立自定义字段参数
Android development with Kotlin programming language II Conditional control
ORACLE ASM提供的三种冗余方式
ansible-playbook配置ssh互信
冬日里,28℃的爱情
Five reasons why developers choose Klocwork, a static analysis tool for code quality, for software security
广告电商系统
Apache APISIX Ingress v1.5-rc1 released
EOS的共识机制与区块生成
Cesium.js 三维土壤地质剖面分割挖掘
Keras 模型多输出 loss weight metrics 设置
el-menu箭头改为右下
五大理由告诉你为什么开发人员选择代码质量静态分析工具Klocwork来实现软件安全