So this is very similar to how we designed the loop queue, and if we use the k + 1 method from the ta, which is the same as 622 and most of the other things, we just deleteFront and insertFront and we have to rewrite it. === this. Max = 1 = isFull = isEmpty = this. Max + 1 = this

Methods a

/* * @lc app=leetcode.cn id=641 lang=javascript * * [641] Design loop double-ended queue */

// @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

Copy the code

Method 2

When I was writing this article, I happened to find this algorithm written by a senior. I looked at it carefully and found it easier to understand than the one written by the ta and the captain. I repeated it by myself according to the code of others, and found that there was no problem and it was easier to understand.

Senior link juejin.cn/post/703545…

I wrote my own code according to other people’s code.

/* * @lc app=leetcode.cn id=641 lang=javascript * * [641] Design loop double-ended queue */

// @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



Copy the code

This code is written with ta, ah, are using arrays to solve why ta can’t use array method, this method two array method can understand, at least I understand