题目描述
设计实现双端队列。 你的实现需要支持以下操作:
MyCircularDeque(k):构造函数,双端队列的大小为k。 insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。 insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。 deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。 deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。 getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。 getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。 isEmpty():检查双端队列是否为空。 isFull():检查双端队列是否满了。 示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3 circularDeque.insertLast(1); // 返回 true circularDeque.insertLast(2); // 返回 true circularDeque.insertFront(3); // 返回 true circularDeque.insertFront(4); // 已经满了,返回 false circularDeque.getRear(); // 返回 2 circularDeque.isFull(); // 返回 true circularDeque.deleteLast(); // 返回 true circularDeque.insertFront(4); // 返回 true circularDeque.getFront(); // 返回 4
提示:
所有值的范围为 [1, 1000] 操作次数的范围为 [1, 1000] 请不要使用内置的双端队列库。
来源:力扣(LeetCode) 链接:leetcode-cn.com/problems/de… 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解思路
该题只是比622设计循环队列的题目多了两个方法,其他的见[路飞]_LeetCode题622设计循环队列 - 掘金 (juejin.cn)
- 从头部添加 只需要先将front指针向前移动一位,然后将value值赋给front位即可。需要注意的是,当做front-1向前移动操作时,由于存在front-1小于0,所以需要加上数组长度,保证front-1为数组长度的有效值。并对数组长度取模即可得出向前移动后的有效添加位置
- 从尾部删除 只需要将rear指针向前移动一位即可完成删除操作。需要注意的是,当做rear-1向前移动操作时,由于存在rear-1小于0,所以需要加上数组长度,保证rear-1为数组长度的有效值。并对数组长度取模即可得出从后删除的有效添加位置
题解代码
* @param {number} k
*/
var MyCircularDeque = function(k) {
this.queue = Array(k+1);//用数组表示队列,数组长度比队列容量多1
this.front = 0;//队列的头指针,初始指向数组的0位
this.rear = 0;//队列的尾指针,初始指向数组的0位
this.max = k;//队列的容量
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function(value) {
if(this.isFull()) return false;//队列满了则不能添加
//从队列前边做添加,需要将front指针先向前移动front-1
//由于是循环队列,存在front-1的数值小于队列容量,
//因此对数组长度取模,保证front的数值在数组的有效值内
this.front = (this.front + this.max) % (this.max + 1);
this.queue[this.front] = value;
return true;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function(value) {
if(this.isFull()) return false;//队列满了则不能添加
this.queue[this.rear] = value;//将队列的rear位赋值value
//rear指针向后移动一位,由于是队列的循环的,
//存在rear+1的数值大于队列容量,
//因此对数组长度取模,保证rear的数值在数组的有效值内
this.rear = (this.rear + 1) % (this.max + 1);
return true;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function() {
if(this.isEmpty()) return false;//空队列无法做删除
this.front = (this.front + 1) % (this.max + 1);//头指针向后移动一位即可从前边删除
return true;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function() {
if(this.isEmpty()) return false;//空队列无法做删除
//让队列头指针向后移动
//存在front + 1的数值大于队列容量,
//因此对数组长度取模,保证front的数值在数组的有效值内
this.rear = (this.rear + this.max)%(this.max + 1);
return true;
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function() {
if(this.isEmpty()) return -1;//空队列返回-1
return this.queue[this.front];//队列非空,front指针处的值即为队首元素
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function() {
if(this.isEmpty()) return -1;//空队列返回-1
//此处实际上应该是return this.queue[(this.rear - 1 + this.max + 1) % (this.max + 1)];
//由于rear处为下一个添加元素位,所以rear - 1是此时队列最后一个元素的位置,
//存在rear+1的数值大于队列容量,
//因此对数组长度取模,保证rear的数值在数组的有效值内
return this.queue[(this.rear + this.max) % (this.max + 1)]
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function() {
//由于rear处为下一个添加元素位,所以rear-front即为队列元素的个数
//由于是循环队列,存在rear<front,所以要+(max+1)保证数值的有效性
//对数组长度取模后元素个数为0即为空队列
return (this.rear - this.front + this.max + 1) % (this.max + 1) === 0;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function() {
//由于rear处为下一个添加元素位,所以rear-front即为队列元素的个数
//由于是循环队列,存在rear<front,所以要+(max+1)保证数值的有效性
//对数组长度取模后元素个数为队列最大容量即为满队列
return (this.rear - this.front + this.max + 1) % (this.max + 1) === this.max;
};