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

news/2025/2/12 22:32:57/

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/news/1570679.html

相关文章

利用Termux在安卓手机中安装 PostgreSQL

利用Termux在安卓手机中安装 PostgreSQL ⬇️Termux下载 点击下载 在 Termux 中安装 PostgreSQL 可以按照以下步骤进行&#xff1a; 1. 更新 Termux 包管理器 先更新软件包列表和已安装的软件包&#xff1a; pkg update && pkg upgrade -y2. 安装 PostgreSQL 使…

Eclipse IDE 快捷键大全

文章目录 简介 ✨常用编辑快捷键 ⌨️基础编辑操作查找和定位代码优化 调试快捷键 &#x1f41b;编辑器通用快捷键 &#x1f4dd;窗口操作快捷键 &#x1fa9f;特殊功能快捷键 &#x1f527;重构相关快捷键 &#x1f504;提示 &#x1f4a1; 简介 ✨ Eclipse 作为一款强大的集…

vue(5)

一.自定义指令 每个指令有着自己各自独立的功能&#xff0c;可以封装一些dom操作&#xff0c;扩展额外功能v-focus、v-loading、v-lazy ①全局注册语法&#xff1a; Vue.directive(指令名&#xff0c;{// 指令名&#xff1a;指令的配置项"inserted "(el) {el.focus(…

6.Python函数:函数定义、函数的类型、函数参数、函数返回值、函数嵌套、局部变量、全局变量、递归函数、匿名函数

1. 函数定义 Python函数通过def关键字定义。一个函数通常包括函数名、参数列表和函数体。 def greet(name):return f"Hello, {name}!"2. 函数的类型 Python中的函数主要有以下几种类型&#xff1a; 普通函数&#xff1a;具有明确的输入参数和返回值。递归函数&am…

哨兵模式与 Redis Cluster:高可用 Redis 的深度剖析

深入探讨 Redis 高可用性解决方案&#xff1a;哨兵模式与 Redis Cluster 一、哨兵模式&#xff08;Redis Sentinel&#xff09;深入解析 &#xff08;一&#xff09;工作原理详解 哨兵模式通过一个或多个哨兵实例监控 Redis 主从复制集群&#xff0c;确保在主节点发生故障时…

【Spring】什么是Spring?

什么是Spring&#xff1f; Spring是一个开源的轻量级框架&#xff0c;是为了简化企业级开发而设计的。我们通常讲的Spring一般指的是Spring Framework。Spring的核心是控制反转(IoC-Inversion of Control)和面向切面编程(AOP-Aspect-Oriented Programming)。这些功能使得开发者…

数据结构及排序算法

数据结构 线性结构 ◆线性结构:每个元素最多只有一个出度和一个入度,表现为一条线状。线性表按存储方式分为顺序表和链表。 存储结构: ◆顺序存储:用一组地址连续的存储单元依次存储线性表中的数据元素,使得逻辑上相邻的元素物理上也相邻。 ◆链式存储:存储各数据元素的结点…

b s架构 网络安全 网络安全架构分析

目录 文章目录 目录网络安全逻辑架构 微分段&#xff08;Micro-segmentation&#xff09;防火墙即服务&#xff08;Firewall asa Service &#xff0c;FWaaS&#xff09;安全网络网关&#xff08;Secure web gateway&#xff09;净化域名系统&#xff08;Sanitized Domain Na…