「这是我参与11月更文挑战的第6天,活动详情查看:2021最后一次更文挑战」
题目
设计循环双端队列
设计实现双端队列。 你的实现需要支持以下操作:
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
思路:
有了上一篇,设计循环队列的经验,这一题思路其实很明确,就是用数组模拟队列,设立一个head指针用于标记头节点的位置,设立一个count用来记录队列成员数量。双端队列只是比循环队列多了从头部添加和从尾部删除的方法而已。
从头部添加
- 首先,将
headIndex左移一步,但是有可能headIndex在0号位,左移要让它能接到尾部去,那么我们可以进行如下计算,this.headIndex = (this.headIndex -1 + this.capacity)%this.capacity。 - 在
headIndex位置增加一个值即可。
从尾部删除
直接进行this.count-- 即可。
代码如下:
/**
* @param {number} k
*/
var MyCircularDeque = function(k) {
this.items = [];
this.capacity = k;
this.headIndex = 0;
this.count = 0;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function(value) {
if(this.count == this.capacity){
return false;
}else{
this.headIndex = (this.headIndex - 1 + this.capacity)%this.capacity;
this.items[this.headIndex] = value;
this.count ++;
return true;
}
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function(value) {
if(this.count == this.capacity){
return false;
}else{
this.items[(this.headIndex + this.count)%this.capacity] = value;
this.count ++;
return true;
}
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function() {
if(this.count == 0){
return false;
}else{
this.headIndex = (this.headIndex + 1)%this.capacity;
this.count --;
return true;
}
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function() {
if(this.count == 0){
return false;
}else{
this.count --;
return true;
}
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function() {
if(this.count == 0){
return -1
}else{
return this.items[this.headIndex]
}
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function() {
if(this.count == 0){
return -1
}else{
return this.items[(this.headIndex + this.count - 1)%this.capacity]
}
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function() {
return this.count == 0;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function() {
return this.count == this.capacity;
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* var obj = new MyCircularDeque(k)
* var param_1 = obj.insertFront(value)
* var param_2 = obj.insertLast(value)
* var param_3 = obj.deleteFront()
* var param_4 = obj.deleteLast()
* var param_5 = obj.getFront()
* var param_6 = obj.getRear()
* var param_7 = obj.isEmpty()
* var param_8 = obj.isFull()
*/