「这是我参与12月更文挑战的第20天,活动详情查看:2021最后一次更文挑战」
641. 设计循环双端队列
设计实现双端队列。 你的实现需要支持以下操作:
- 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]
- 请不要使用内置的双端队列库。
数组
思路
双端队列是一种队列和栈结合的数据结构,即可以从队列的前后插入元素,也可以从队列的前后取出元素
数组就可以直接用来实现队列
- insertFront:通过unshift往数组中增加元素
- insertLast:通过push往数组中增加元素
- deleteFront:通过shift直接删除数组的第一个元素
- deleteLast:通过pop直接删除数组的第一个元素
- getFront:获取数组的第一个值arr[0]
- getRear:获取数组的最后一个值arr[arr.length]
- isEmpty:判断数组的length是否为0
- isFull: 判断数组的长度是否===k
/**
* @param {number} this.k
*/
var MyCircularDeque = function (k) {
this.k = k
this.circularDeque = []
this.length = 0
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function (value) {
if (this.circularDeque.length >= this.k) return false
this.circularDeque.unshift(value)
return true
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function (value) {
if (this.circularDeque.length >= this.k) return false
this.circularDeque.push(value)
return true
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function () {
if (this.circularDeque.length === 0) return false
this.circularDeque.shift()
return true
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function () {
if (this.circularDeque.length === 0) return false
this.circularDeque.pop()
return true
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function () {
if (this.isEmpty()) return -1
return this.circularDeque[0]
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function () {
if (this.isEmpty()) return -1
return this.circularDeque[this.circularDeque.length - 1]
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function () {
return this.circularDeque.length === 0
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function () {
return this.circularDeque.length === this.k
};
/**
* 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()
*/