题目描述
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
分析
输入:k 作为队列的最大长度
输出:一个实例具备题目要求的队列方法
解题思路
题目要求我们设计一种首尾相连的数据结构,首先想到的是环,因为 JS 没有这种数据结构,所以我们可以用一个数组来存储数据,然后用两个指针 head, tail 来指向队列的头和尾,在每次增加,删除元素的时候改变他的指针就可以了。
对于一个数据结构,最重要的是他的属性。对于这题的循环队列而言,肯定需要一个 queue 属性去存数组,我们的数据都存在这里。
还需要一个属性表示队列当前长度,我把它设为 count,队列的容量 capacipty。
最后是指针这部分,我们没必要保存两个 index 去表示头和尾,实际上只需要保存一个 headIndex,尾部的索引完全可以通过计算得出:
tailIndex = (headIndex + count - 1) % capacity
相当于是,让 headIndex 向右移动一些位置,直到到达 tail 的位置,然后如果到队列尾部了,就从头开是再去移动剩下的步数。
维护过多的变量,更容易导致 bug,我们只需要在用到的时候,计算一下就可以了。
我的建议是先写出判断“满”和“空”的方法,因为其他地方也会用到:
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function () {
return !this.count;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function () {
return this.count === this.capacity;
};
很简单,观察 count 和 capacity 是否相等可以判断是否已满。看 count 是否为 0,可以判断是否是空的队列。
然后看增加:
/**
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function (value) {
if (this.isFull()) return false;
this.queue[(this.headIndex + this.count) % this.capacity] = value;
this.count++;
return true;
};
对于向队列里增加一个元素,
首先判断是否为满,如果是的话直接 return false。
然后是通过计算找到 tailIndex
最后在这个 index 的数组位置设置 value,return true 我们就增加了元素。
对于删除:
/**
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function () {
if (this.isEmpty()) return false;
this.queue[this.headIndex] = undefined;
this.count--;
this.headIndex = (this.headIndex + 1) % this.capacity;
return true;
};
首先要判断是否为空
然后让 headIndex 为 undefined,也就是删除元素
减小 count,队列当前长度
让 headIndex 向后移动一位,因为要考虑到 headIndex 已经到达数组尾部的情况,所以让 headIndex + 1 取容量的模
最后 return true
对于找头尾,不再赘述
代码
/**
* @param {number} k
*/
var MyCircularQueue = function (k) {
this.queue = [];
this.headIndex = 0;
this.count = 0;
this.capacity = k;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function (value) {
if (this.isFull()) return false;
this.queue[(this.headIndex + this.count) % this.capacity] = value;
this.count++;
return true;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function () {
if (this.isEmpty()) return false;
this.queue[this.headIndex] = undefined;
this.count--;
this.headIndex = (this.headIndex + 1) % this.capacity;
return true;
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Front = function () {
if (this.isEmpty()) return -1;
return this.queue[this.headIndex];
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Rear = function () {
if (this.isEmpty()) return -1;
return this.queue[(this.headIndex + this.count - 1) % this.capacity];
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function () {
return !this.count;
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function () {
return this.count === this.capacity;
};
/**
* 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()
*/
复杂度
时间:O(1),所有操作都是常量级别的复杂度
空间:O(N),需要用数组存储所有数据