设计循环队列(中等难度)

题目概述(简单难度)

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 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

题目链接
点我进入leetcode

思路与代码

思路展现

这道题目的思路我全部放到了我的这篇博客里:大家可以直接戳下面的链接进行跳转查看:
戳我戳我

代码示例

public class MyCircularQueue {
    
    
    private int front;
    //rear表示当前可以插入元素的数组的下标
    private int rear;
    private int[] elem;

    public MyCircularQueue(int k) {
    
    
        this.elem = new int[k+1];
    }

    /**
     * 向循环队列插入一个元素。如果成功插入则返回真。
     *
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
    
    
        //此时代表队列已满,无法再进行插入
        if (isFull()) {
    
    
            return false;
        }
        //放到数组的rear下标
        this.elem[this.rear] = value;
        //rear指针往后走,取余是为了循环起来
        this.rear = (this.rear + 1) % this.elem.length;
        return true;
    }

    public boolean deQueue() {
    
    
        if (isEmpty()) {
    
    
            return false;
        }
        //只需要挪动front这个下标就好了,取余是为了循环起来
        this.front = (this.front + 1) % this.elem.length;
        return true;
    }

    /**
     * 得到队头元素
     *
     * @return
     */
    public int Front() {
    
    
        if (isEmpty()) {
    
    
            return -1;
        }
        return this.elem[this.front];
    }

    /**
     * 得到队尾元素
     * 注意特殊情况
     *
     * @return
     */
    public int Rear() {
    
    
        if (isEmpty()) {
    
    
            return -1;
        }
        int index = -1;
        if (this.rear == 0) {
    
    
            index = this.elem.length - 1;
        } else {
    
    
            index = this.rear - 1;
        }
        return this.elem[index];
    }

    /**
     * 队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
    
    
        return this.rear == this.front;
    }

    /**
     * 队列是否为满
     *
     * @return
     */
    public boolean isFull() {
    
    
        return (this.rear + 1) % this.elem.length == this.front;
    }
}

注意此处的构造方法中构造循环队列底层数组的时候的大小的时候必须是k+1,不能为k,因为一旦是k的话,leetcode会报错:报错信息如下所示:
输入:

[“MyCircularQueue”,“enQueue”,“enQueue”,“enQueue”,“enQueue”,“Rear”,“isFull”,“deQueue”,“enQueue”,“Rear”]
[[3],[1],[2],[3],[4],[],[],[],[4],[]]

输出

[null,true,true,false,false,2,true,true,true,4]

预期结果

[null,true,true,true,false,3,true,true,true,4]

我们可以看到题目所给的测试用例中当插入2的时候我们的代码运行出来是false,题目想要的是true,到了再插入下一个3的时候,题目想要的是才是false,就说明此时循环队列的底层数组想要插入的元素是三个,但是假如此时我们的数组大小设置为k的话,只能插入2个元素,所以要想插入3个元素,初始化数组大小的时候必须的是k+1才可以.

总结

考察对于循环队列的掌握,非常经典的一道题目,大家可以细细品味.

Guess you like

Origin blog.csdn.net/qq_41972686/article/details/120604055