网络异常,图片无法展示
|
「这是我参与11月更文挑战的第23天,活动详情查看:2021最后一次更文挑战」
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 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 的范围内;
- 请不要使用内置的队列库。
本题的循环队列其实就像是一个有固定座位数量的餐桌,来吃饭的人依次坐好,当坐满人之后,新来的人坐不下只能离开或者在一旁排队
当先来的人吃饱离开后,此时空出座位,后续的人就可以坐下吃饭了(这里严格遵循先到先吃饱离开的规则😃)
过程如下:
网络异常,图片无法展示
|
理解了循环队列的性质后,如何设计该数据结构呢?
思路如下:
- 首先构造函数中,根据传入值记录队列长度
max
。接下来创建空数组queue
存储队列中的值,并创建head
、end
指针初初始化指向-1
,创建size
属性初始化为0
- 插入操作中,判断队列是否已满,如果队列已满,返回false。否则判断
head===-1
,如果为真,说明队列是初始化状态,此时新插入的值一定在下标0位置,将head = 0
。然后调整end
指针向后走一步,这里要判断end
指针是否在数组末尾,如果在数组末尾,则需要将end = 0
。最后将value
插入到end
指针所在位置即可,this.size++
- 删除操作首先需要判断队列是否为空,如果为空,返回
false
。否则只需要调整head
指针向后走一步即可。同样需要判断此时head
指针是否在数组末尾,如果是,则head = 0
。最后不要忘记this.size--
- 获取队首元素首先判断队列是否为空,如果为空,返回
-1
,否则返回this.queue[this.head]
即可 - 获取队尾元素首先判断队列是否为空,如果为空,返回
-1
,否则返回this.queue[this.end]
即可 - 判断队列是否为空只需要判断
this.size === 0
即可 - 判断队列是否已满只需要判断
this.size === this.max
即可
以本题示例为例,以上过程如下:
网络异常,图片无法展示
|
代码如下:
var MyCircularQueue = function(k) { // 记录长度 this.max = k; // 队列 this.queue = []; // 头指针 this.head = -1; // 尾指针 this.end = -1; // 队列中元素数量 this.size = 0; }; /** * @param {number} value * @return {boolean} */ MyCircularQueue.prototype.enQueue = function(value) { if(this.isFull()) return false; // 如果此时为初始化状态,第一个元素放在下标0位置,更新head指针 if(this.head === -1) this.head = 0; // end指针向后走一步 if(this.end === this.max-1) this.end = 0; else this.end++; // 更新 size this.size++; // 将 value 放到更新后 end 指针所在位置 this.queue[this.end] = value; return true; }; /** * @return {boolean} */ MyCircularQueue.prototype.deQueue = function() { if(this.isEmpty()) return false; // 删除操作只需要调整头指针向后走一步即可 if(this.head === this.max-1) this.head = 0; else this.head++; // 更新 size this.size--; return true; }; /** * @return {number} */ MyCircularQueue.prototype.Front = function() { if(this.isEmpty()) return -1; return this.queue[this.head] }; /** * @return {number} */ MyCircularQueue.prototype.Rear = function() { if(this.isEmpty()) return -1; return this.queue[this.end] }; /** * @return {boolean} */ MyCircularQueue.prototype.isEmpty = function() { return this.size === 0 }; /** * @return {boolean} */ MyCircularQueue.prototype.isFull = function() { return this.size === this.max }; 复制代码
至此我们就完成了 leetcode-622-设计循环队列
如有任何问题或建议,欢迎留言讨论!