leetcode641. 设计循环双端队列
设计实现双端队列。
实现
MyCircularDeque
类:
MyCircularDeque(int k)
:构造函数,双端队列最大为k
。boolean insertFront()
:将一个元素添加到双端队列头部。 如果操作成功返回true
,否则返回false
。boolean insertLast()
:将一个元素添加到双端队列尾部。如果操作成功返回true
,否则返回false
。boolean deleteFront()
:从双端队列头部删除一个元素。 如果操作成功返回true
,否则返回false
。boolean 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]
/*** Initialize your data structure here. Set the size of the deque to be k.* @param {number} k*/
var MyCircularDeque = function(k) {this.queue = new Array(k+1);//用来模拟队列的数组this.front = 0;//头指针this.rear = 0;//尾指针this.max = k;//记录当前最大容量
};/*** Adds an item at the front of Deque. Return true if the operation is successful. * @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.queue[this.front] = value;return true;
};/*** Adds an item at the rear of Deque. Return true if the operation is successful. * @param {number} value* @return {boolean}*/
MyCircularDeque.prototype.insertLast = function(value) {if(this.isFull()) return false;this.queue[this.rear] = value;this.rear = (this.rear + 1) % (this.max + 1);return true;
};/*** Deletes an item from the front of Deque. Return true if the operation is successful.* @return {boolean}*/
MyCircularDeque.prototype.deleteFront = function() {if(this.isEmpty()) return false;// 删除的队列头部,this.front往后移动一位this.front = (this.front + 1) % (this.max + 1);return true;
};/*** Deletes an item from the rear of Deque. Return true if the operation is successful.* @return {boolean}*/
MyCircularDeque.prototype.deleteLast = function() {if(this.isEmpty()) return false;// 删除队列的最后一个元素,那么尾指针rear,往前移动一位this.rear = (this.rear+this.max) % (this.max + 1);return true;
};/*** Get the front item from the deque.* @return {number}*/
MyCircularDeque.prototype.getFront = function() {if(this.isEmpty()) return -1;return this.queue[this.front]
};/*** Get the last item from the deque.* @return {number}*/
MyCircularDeque.prototype.getRear = function() {if(this.isEmpty()) return -1;return this.queue[(this.rear+this.max) % (this.max + 1)];
};/*** Checks whether the circular deque is empty or not.* @return {boolean}*/
MyCircularDeque.prototype.isEmpty = function() {return ((this.rear - this.front + this.max+1) % (this.max+1)) == 0;
};/*** Checks whether the circular deque is full or not.* @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()*/