这题跟之前那个设计循环队列很想,用助教的k + 1这个方法的话,跟622其他大多都一样,就deleteFront,和insertFront需要重新写.比这助教的方法自己尝试着理解盲写的代码,有时判断isFull和isEmpty的时候=== this.max,自己写成this.max + 1看了几遍都没发现,以为这最简单,还是不是完全理解啊
方法一
/*
* @lc app=leetcode.cn id=641 lang=javascript
*
* [641] 设计循环双端队列
*/
// @lc code=start
/**
* @param {number} k
*/
var MyCircularDeque = function (k) {
this.q = Array(k + 1);
this.front = 0;
this.rear = 0;
this.max = k;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function (value) {
if (this.isFull()) {
return false;
}
this.front = (this.front + this.max) % (this.max + 1);
this.q[this.front] = value;
return true;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function (value) {
if (this.isFull()) {
return false;
}
this.q[this.rear] = value;
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;
}
this.rear = (this.rear + this.max) % (this.max + 1);
return true;
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function () {
if (this.isEmpty()) {
return -1;
}
return this.q[this.front];
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function () {
if (this.isEmpty()) {
return -1;
}
return this.q[(this.rear + this.max) % (this.max + 1)];
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function () {
return (this.rear - this.front + this.max + 1) % (this.max + 1) === 0;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function () {
return (this.rear - this.front + this.max + 1) % (this.max + 1) === this.max;
};
/**
* 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()
*/
// @lc code=end
方法二
在写这文章的时候,偶然发现一个前辈写的这个算法,我仔细看了比助教和船长写的都好理解,我自己又按照人家的代码,撸了一遍,发现没问题,更好理解,
我自己按照人家的代码,自己盲写代码如下,
/*
* @lc app=leetcode.cn id=641 lang=javascript
*
* [641] 设计循环双端队列
*/
// @lc code=start
/**
* @param {number} k
*/
var MyCircularDeque = function (k) {
this.arr = [];
this.max = k;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function (value) {
if (this.isFull()) {
return false;
}
this.arr.unshift(value)
return true;
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function (value) {
if (this.isFull()) {
return false;
}
this.arr.push(value)
return true;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function () {
if (this.isEmpty()) {
return false;
}
this.arr.shift()
return true;
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function () {
if (this.isEmpty()) {
return false;
}
this.arr.pop()
return true;
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function () {
if (this.isEmpty()) {
return -1;
}
return this.arr[0]
};
/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function () {
if (this.isEmpty()) {
return -1;
}
return this.arr[this.arr.length - 1]
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function () {
return this.arr.length === 0
};
/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function () {
return this.arr.length === this.max
};
/**
* 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()
*/
// @lc code=end
这个代码跟助教写的,哎, 都是用数组解决的 为啥助教不能用数组的方法了,这方法二引用数组的方法都能理解把,最起码 我理解了