算法14(力扣622)设置循环队列

server/2025/2/11 5:19:23/

1、问题

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

  • MyCircularQueue(k): 构造器,设置队列长度为 k 。
  • Front: 从队首获取元素。如果队列为空,返回 -1 。
  • Rear: 获取队尾元素。如果队列为空,返回 -1 。
  • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  • isEmpty(): 检查循环队列是否为空。
  • isFull(): 检查循环队列是否已满。

2、示例

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

3、理解题意

        题意:利用数组实现循环队列的一下方法

4、具体步骤

(1)数组构建循环队列需要哪些元素?数组queue、头指针front、尾指针rear、capacity队列最大容量、size队列中当前元素数量

(2)判断队列是否为空,直接判断当前元素size是否为0

(3)判断队是否满,直接判断当前元素数size是否和队列的最大长度相同

(4)插入:

        1)判断队满,满则返回。

        2)判断队空?空,头指针前移:非空,尾指针前移,插入,当前元素数+1

(5)删除
        1)判空?空,返回:非空(最后一个元素?是,重置头、尾指针:否,头指针前移,当前元素-1)


(6)从队首获取元素
        1)判空?空,返回-1:非空,返回头指针指向的元素


(7)从队尾获取元素
        1)空?空,返回-1:非空,返回尾指针指向的元素

5、完整代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>设计循环队列</title></head><body><p>设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。</p><p>循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。</p><p><h4>你的实现应该支持如下操作:</h4>MyCircularQueue(k): 构造器,设置队列长度为 k 。<br>Front: 从队首获取元素。如果队列为空,返回 -1 。<br>Rear: 获取队尾元素。如果队列为空,返回 -1 。<br>enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。<br>deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。<br>isEmpty(): 检查循环队列是否为空。<br>isFull(): 检查循环队列是否已满。<br></p></body><script>/*** @param {number} k*/var MyCircularQueue = function(k) {this.queue = new Array(k) // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1this.front = -1this.rear = -1// 当前元素数量this.size = 0 // 队列最大容量this.capacity = k};/** * @param {number} value* @return {boolean}*/MyCircularQueue.prototype.enQueue = function(value) {// 判断队列是否为空,如果为空,头指针需要向前移动if (this.isEmpty()) {this.front = 0}if (this.isFull()) {console.log(false,this.queue);return false}// 添加// 尾指针向前this.rear = (this.rear + 1)%this.capacity// 添加this.queue[this.rear] = valuethis.size++console.log(true,this.queue);return true};/*** @return {boolean}*/MyCircularQueue.prototype.deQueue = function() {// if (this.isEmpty()) {console.log('false',this.queue);return false}if (this.front===this.rear) {// 队列中只有一个元素且该元素即将被删除this.front = -1this.rear = -1}else{// 头指针前移,删除元素this.front = (this.front+1)%this.capacity}this.size--;console.log(true,this.queue);return true};/*** @return {number}*/MyCircularQueue.prototype.Front = function() {if (this.isEmpty()) {console.log('-1',this.queue);return -1}console.log(this.queue[this.front]);return this.queue[this.front]};/*** @return {number}*/MyCircularQueue.prototype.Rear = function() {if (this.isEmpty()) {return -1}console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);// this.rear + this.capacity确保当 this.rear 为 0 时,计算出的索引不会是负数return this.queue[(this.rear + this.capacity ) % this.capacity]};/*** @return {boolean}*/MyCircularQueue.prototype.isEmpty = function() {if (this.size === 0) {return true}return false};/*** @return {boolean}*/MyCircularQueue.prototype.isFull = function() {if (this.size === this.capacity) {return true}return false};var circularQueue = new MyCircularQueue(3); // 设置长度为 3circularQueue.enQueue(1);  // 返回 truecircularQueue.enQueue(2);  // 返回 truecircularQueue.enQueue(3);  // 返回 truecircularQueue.enQueue(4);  // 返回 false,队列circularQueue.Rear();  // 返回 3circularQueue.isFull();  // 返回 truecircularQueue.deQueue();  // 返回 truecircularQueue.enQueue(4);  // 返回 truecircularQueue.Rear();  // 返回 4/** * Your MyCircularQueue object will be instantiated and called as such:* var obj = new MyCircularQueue(k)* var param_1 = obj.enQueue(value)* var param_2 = obj.deQueue()* var param_3 = obj.Front()* var param_4 = obj.Rear()* var param_5 = obj.isEmpty()* var param_6 = obj.isFull()*/</script>
</html>

6、力扣通过代码

    var MyCircularQueue = function(k) {this.queue = new Array(k) // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1this.front = -1this.rear = -1// 当前元素数量this.size = 0 // 队列最大容量this.capacity = k};/** * @param {number} value* @return {boolean}*/MyCircularQueue.prototype.enQueue = function(value) {// 判断队列是否为空,如果为空,头指针需要向前移动if (this.isEmpty()) {this.front = 0}if (this.isFull()) {console.log(false,this.queue);return false}// 添加// 尾指针向前this.rear = (this.rear + 1)%this.capacity// 添加this.queue[this.rear] = valuethis.size++console.log(true,this.queue);return true};/*** @return {boolean}*/MyCircularQueue.prototype.deQueue = function() {// if (this.isEmpty()) {console.log('false',this.queue);return false}if (this.front===this.rear) {// 队列中只有一个元素且该元素即将被删除this.front = -1this.rear = -1}else{// 头指针前移,删除元素this.front = (this.front+1)%this.capacity}this.size--;console.log(true,this.queue);return true};/*** @return {number}*/MyCircularQueue.prototype.Front = function() {if (this.isEmpty()) {console.log('-1',this.queue);return -1}console.log(this.queue[this.front]);return this.queue[this.front]};/*** @return {number}*/MyCircularQueue.prototype.Rear = function() {if (this.isEmpty()) {return -1}console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);return this.queue[(this.rear + this.capacity) % this.capacity]};/*** @return {boolean}*/MyCircularQueue.prototype.isEmpty = function() {if (this.size === 0) {return true}return false};/*** @return {boolean}*/MyCircularQueue.prototype.isFull = function() {if (this.size === this.capacity) {return true}return false};


http://www.ppmy.cn/server/166666.html

相关文章

Kubernetes之kube-proxy运行机制分析

一、基础知识 1.Kubernetes再创建服务时会为服务分配一个虚拟IP地址&#xff0c;客户端通过这个虚拟Ip地址来访问服务&#xff0c;而服务则负责将请求转发到后端pod上。 2.上述阐述的过程为一个反向代理的过程&#xff0c;但是这个反向代理和普通的反向代理的区别是它的IP地址是…

html转PDF文件最完美的方案(wkhtmltopdf)

目录 需求 一、方案调研 二、wkhtmltopdf使用 如何使用 文档简要说明 三、后端服务 四、前端服务 往期回顾 需求 最近在做报表类的统计项目&#xff0c;其中有很多指标需要汇总&#xff0c;网页内容有大量的echart图表&#xff0c;做成一个网页去浏览&#xff0c;同时…

NetCore Consul动态伸缩+Ocelot 网关 缓存 自定义缓存 + 限流、熔断、超时 等服务治理 + ids4鉴权

网关 OcelotGeteway 网关 Ocelot配置文件 {//单地址多实例负载均衡Consul 实现动态伸缩"Routes": [{// 上游 》》 接受的请求//上游请求方法,可以设置特定的 HTTP 方法列表或设置空列表以允许其中任何方法"UpstreamHttpMethod": [ "Get", &quo…

【R语言】卡方检验

一、定义 卡方检验是用来检验样本观测次数与理论或总体次数之间差异性的推断性统计方法&#xff0c;其原理是比较观测值与理论值之间的差异。两者之间的差异越小&#xff0c;检验的结果越不容易达到显著水平&#xff1b;反之&#xff0c;检验结果越可能达到显著水平。 二、用…

洛谷 P2095 营养膳食 C语言

P2095 营养膳食 - 洛谷 | 计算机科学教育新生态 题目描述 Mr.L 正在完成自己的增肥计划。 为了增肥&#xff0c;Mr.L 希望吃到更多的脂肪&#xff0c;然而也不能只吃高脂肪食品&#xff0c;那样的话就会导致缺少其他营养。 Mr.L 通过研究发现&#xff1a;真正的营养膳食规定…

青少年编程与数学 02-009 Django 5 Web 编程 03课题、项目结构

青少年编程与数学 02-009 Django 5 Web 编程 03课题、项目结构 一、项目结构项目根目录应用目录其他目录 二、项目设置Django 插件设置项目配置环境变量设置项目目录标记版本控制 三、Django 插件安装 Django 插件配置 Django 插件使用 Django 插件功能 四、扩展插件开发效率插…

未来AI医院蓝图:源码、机器人与数字孪生如何打造智能医疗APP?

在人工智能&#xff08;AI&#xff09;、物联网&#xff08;IoT&#xff09;和大数据技术的推动下&#xff0c;医疗行业正在经历一场深刻的变革。从传统医院到互联网医院&#xff0c;再到智能医疗生态的构建&#xff0c;未来的AI医院不仅能提供更高效的医疗服务&#xff0c;还能…

ProcessingP5js游戏掉落的恐龙蛋

这款游戏是一款趣味十足的物品接取游戏&#xff0c;玩家将扮演一个接物品的角色。游戏的目标是通过控制篮子左右移动&#xff0c;接住从天而降的恐龙蛋和其他物品&#xff0c;积累分数&#xff0c;同时避开掉落的损失道具&#xff0c;确保自己的分数不断增长。 游戏玩法非常简…