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

news/2025/2/9 5:49:55 标签: leetcode, 算法, 职场和发展

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) 
        // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
        this.front = -1
        this.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] = value
        this.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 = -1
            this.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); // 设置长度为 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
    /** 
    * 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) 
        // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
        this.front = -1
        this.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] = value
        this.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 = -1
            this.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.niftyadmin.cn/n/5845617.html

相关文章

优惠券平台(十五):实现兑换/秒杀优惠券功能(2)

业务背景 在上一节中&#xff0c;我们介绍了通过数据库扣减完成用户兑换优惠券的逻辑&#xff0c;这种方式虽然稳妥&#xff0c;但性能有所不足&#xff0c;因为主流程的操作是同步执行的&#xff0c;导致响应时间变长&#xff0c;吞吐量下降。在本章节中&#xff0c;我们通过…

基于对比增强的超声视频的域知识为乳腺癌诊断提供了深度学习

Domain Knowledge Powered Deep Learning for Breast Cancer Diagnosis Based on Contrast-Enhanced Ultrasound Videos 期刊分析摘要引言相关工作乳腺癌中的CAD基于乳房CEU的CAD方法整体框架原始C3D骨干领域知识指导的时间注意模块(DKG-TMA)域知识引导的通道注意模块数据集和实…

python 包和模块的导入机制详解!

油管看到一个非常好的视频&#xff0c;在这里对一些视频内的重点内容进行总结。 注&#xff1a;本文主要供自己复习使用&#xff0c;仅提供个人认为的重点内容&#xff0c;难免有不周到之处&#xff0c;如果想要详细地了解相关机制&#xff0c;请在油管搜索&#xff1a; Pytho…

前端布局与交互实现技巧

前端布局与交互实现技巧 1. 保持盒子在中间位置 在网页设计中&#xff0c;经常需要将某个元素居中显示。以下是一种常见的实现方式&#xff1a; HTML 结构 <!doctype html> <html lang"en"> <head><meta charset"UTF-8"><m…

后台管理系统网页开发

CSS样式代码 /* 后台管理系统样式文件 */ #container{ width:100%; height:100%; /* background-color:antiquewhite;*/ display:flex;} /* 左侧导航区域:宽度300px*/ .left{ width:300px; height: 100%; background-color:#203453; display:flex; flex-direction:column; jus…

MySQL中有哪几种锁?

大家好&#xff0c;我是锋哥。今天分享关于【MySQL中有哪几种锁&#xff1f;】面试题。希望对大家有帮助&#xff1b; MySQL中有哪几种锁&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 在MySQL中&#xff0c;锁的种类主要有以下几种&#xff0c;主要用…

国产编辑器EverEdit - Web预览功能

1 Web预览 1.1 应用场景 在编辑HTML文件时&#xff0c;可以通过EverEdit的Web预览功能&#xff0c;方便用户随时观察和调整HTML代码。 1.2 使用方法 1.2.1 使用EverEdit内部浏览器预览 选择主菜单查看 -> Web预览&#xff0c;或使用快捷键Ctrl B&#xff0c;即可打开Ev…

复原IP地址(力扣93)

有了上一道题分割字符串的基础&#xff0c;这道题理解起来就会容易很多。相同的思想我就不再赘述&#xff0c;在这里我就说明一下此题额外需要注意的点。首先是终止条件如何确定&#xff0c;上一题我们递归到超过字符串长度时&#xff0c;则说明字符串已经分割完毕&#xff0c;…