[路飞]_算法成长之路十五,设计循环双端队列

77 阅读2分钟

个人算法成长之路十五!!!定期更新一些刷题过程中个人的思路以及理解。有兴趣的朋友们可以互动交流哈~

题目:

leetcode-641. 设计循环双端队列

设计实现双端队列。

你的实现需要支持以下操作:

MyCircularDeque(k):构造函数,双端队列的大小为k。

insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。

insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。

deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。

deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。

getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。

getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。

isEmpty():检查双端队列是否为空。

isFull():检查双端队列是否满了。

解题思路:

/**
 * Initialize your data structure here. Set the size of the deque to be k.
 * @param {number} k
 */
var MyCircularDeque = function(k) {
    this.ArrDeque = new Array(k+1);
    this.front = 0;
    this.rear = 0;
    this.n = k+1;
    this.k = 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('front')){
        return false;
    }else{
        this.front = (--this.front+this.n)%(this.n);
        this.ArrDeque[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('last')){
        return false;
    }else{
        this.ArrDeque[this.rear] = value; 
        this.rear = (this.rear+1)%(this.n);
        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;
    }else{
        this.front = (++this.front)%(this.n);
        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
    }else{
        this.rear = (--this.rear+this.n)%(this.n);
        return true;
    }
};

/**
 * Get the front item from the deque.
 * @return {number}
 */
MyCircularDeque.prototype.getFront = function() {
    return this.isEmpty() ? -1 : this.ArrDeque[this.front]
};

/**
 * Get the last item from the deque.
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
    return this.isEmpty() ? -1 : this.ArrDeque[(this.rear-1+this.n)%(this.n)]
};

/**
 * Checks whether the circular deque is empty or not.
 * @return {boolean}
 */
MyCircularDeque.prototype.isEmpty = function() {
    if(this.front == this.rear){
        return true;
    }else{
        return false;
    }
};

/**
 * Checks whether the circular deque is full or not.
 * @return {boolean}
 */
MyCircularDeque.prototype.isFull = function(x) {
    return ( this.rear - this.front + this.n)%(this.n) == this.k;
};