携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第17天,点击查看活动详情
设计实现双端队列。
实现 MyCircularDeque 类:
MyCircularDeque(int k):构造函数,双端队列最大为kboolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回true,否则返回false。boolean insertLast():将一个元素添加到双端队列尾部。如果操作成功返回true,否则返回falseboolean deleteFront():从双端队列头部删除一个元素。 如果操作成功返回true,否则返回falseboolean deleteLast():从双端队列尾部删除一个元素。如果操作成功返回true,否则返回false。int getFront()):从双端队列头部获得一个元素。如果双端队列为空,返回-1。int getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回-1。boolean isEmpty():若双端队列为空,则返回true,否则返回false。boolean isFull():若双端队列满了,则返回true,否则返回false。
示例 1:
输入
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
输出
[null, true, true, true, false, 2, true, true, true, 4]
解释
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
数组
对于一个固定大小的数组,只要知道队尾 与队首 ,即可计算出队列当前的长度:
循环双端队列与循环队列的属性一致:
- elements:一个固定大小的数组,用于保存循环队列的元素。
- capacity:循环队列的容量,即队列中最多可以容纳的元素数量。
- front:队列首元素对应的数组的索引。
- rear:队列尾元素对应的索引的下一个索引。
循环双端队列的接口方法如下:
- MyCircularDeque(int k):初始化队列,同时 数组的空间初始化大小为 。 全部初始化为 。
- insertFront(int value):队列未满时,在队首插入一个元素。我们首先将队首 移动一个位置,更新队首索引为 更新为 。
- insertLast(int value):队列未满时,在队列的尾部插入一个元素,并同时将队尾的索引 rear\textit{rear}rear 更新为。
- deleteFront():队列不为空时,从队首删除一个元素,并同时将队首的索引 更新为 。
- deleteLast():队列不为空时,从队尾删除一个元素。并同时将队尾的索引 更新为 。
- getFront():返回队首的元素,需要检测队列是否为空。
- getRear():返回队尾的元素,需要检测队列是否为空。
- isEmpty():检测队列是否为空,根据之前的定义只需判断 是否等于 。
- isFull():检测队列是否已满,根据之前的定义只需判断 是否等于 。
var MyCircularDeque = function(k) {
this.capacity = k + 1;
this.rear = this.front = 0;
this.elements = new Array(k + 1).fill(0);
};
MyCircularDeque.prototype.insertFront = function(value) {
if (this.isFull()) {
return false;
}
this.front = (this.front - 1 + this.capacity) % this.capacity;
this.elements[this.front] = value;
return true;
};
MyCircularDeque.prototype.insertLast = function(value) {
if (this.isFull()) {
return false;
}
this.elements[this.rear] = value;
this.rear = (this.rear + 1) % this.capacity;
return true;
};
MyCircularDeque.prototype.deleteFront = function() {
if (this.isEmpty()) {
return false;
}
this.front = (this.front + 1) % this.capacity;
return true;
};
MyCircularDeque.prototype.deleteLast = function() {
if (this.isEmpty()) {
return false;
}
this.rear = (this.rear - 1 + this.capacity) % this.capacity;
return true;
};
MyCircularDeque.prototype.getFront = function() {
if (this.isEmpty()) {
return -1;
}
return this.elements[this.front];
};
MyCircularDeque.prototype.getRear = function() {
if (this.isEmpty()) {
return -1;
}
return this.elements[(this.rear - 1 + this.capacity) % this.capacity];
};
MyCircularDeque.prototype.isEmpty = function() {
return this.rear == this.front;
};
MyCircularDeque.prototype.isFull = function() {
return (this.rear + 1) % this.capacity == this.front;
};