[路飞]_leetcode 641. 设计双向循环队列 JavaScript版

418 阅读2分钟

「这是我参与11月更文挑战的第 10 天,活动详情查看:2021最后一次更文挑战

题目

题目来源:641. 设计双向循环队列

设计实现双端队列。

你的实现需要支持以下操作:
vMyCircularDeque(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]
请不要使用内置的双端队列库。

  • 首先创建容量为k的数组,用来存储数据

  • 定义变量:

  • capacity 表示队列最大容量

  • front 表示队首的索引

  • rear 表示队尾的索引 它指向下一个从队尾入队元素的位置

  • count 表示当前队列长度

  • capacity 初始值为k

  • frony 初始值为0

  • rear 初始值为0

  • count 初始值为0,count == 0 当前队列为空

1636629806(1).png

  • 从队尾添加一个元素时,向queue[rear]位置插入元素

  • 然后队尾索引向后移动rear=(rear+1)%capacity,并且count++

1636629980(1).png

  • 从队首删除一个元素时,front向后移动即可,front=(front+1)%capacity,count--

1636630263(1).png

  • 从队尾添加一个元素时,rear = (rear+1) % capacity,并且count++

1636630238(1).png

  • 从对首添加一个元素,front先向前移动一位,front=(front-1+capacity)%capacity,并且count++

1636630581(1).png

  • 此时继续添加元素,当capacitycount相等时,说明队列已满.

1636630664(1).png

代码实现

/**
 * @param {number} k
 */
var MyCircularDeque = function(k) {
    this.queue = new 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.queue[this.front] = value
    return true
};

/** 
 * @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
};

/**
 * @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;
    // 删除队列元素的最后一个元素,那么尾指针rear,往前移动一位
    this.rear = (this.rear + this.max) % (this.max + 1)
    return true
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getFront = function() {
    if(this.isEmpty()) return -1;
    return this.queue[this.front]
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
    if(this.isEmpty()) return -1;
    return this.queue[(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
};