LeetCode探索(122):641-设计循环双端队列

146 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第19天,点击查看活动详情 >>

题目

设计实现双端队列。

实现 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]

解释
var 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 <= k <= 1000
  • 0 <= value <= 1000
  • insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000

思考

本题难度中等。

首先是读懂题意。我们需要设计实现双端队列的 MyCircularDeque 类,拥有insertFront, insertLast, getFront, getRear, isEmpty, isFull等方法。

根据题目说明,我们添加代码逻辑,比如getFront() 方法是从双端队列头部获得一个元素,如果双端队列为空,即isEmpty()为 true 则返回 -1 。接着,我们定义 elements 数组用于保存循环队列的元素,数组的长度为 k + 1,k 为给定的队列元素数目。定义数字 capacity 为循环队列的容量。定义 front 为队列首元素对应的数组的索引,rear 为队列尾元素对应的索引的下一个索引。根据循环队列的定义,队列为空的条件是 front === rear,而队列为满的条件是 front === (rear+1) mod capacity

解答

方法一:数组

/**
 * @param {number} k
 */
var MyCircularDeque = function(k) {
  this.capacity = k + 1
  this.elements = new Array(k + 1).fill(0)
  this.rear = this.front = 0
}

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertFront = function(value) {
  if (this.isFull()) {
    return false
  }
  this.front = (this.front - 1 + this.capacity) % this.capacity
  this.elements[this.front] = value
  return true
}

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertLast = function(value) {
  if (this.isFull()) {
    return false
  }
  this.elements[this.rear] = value
  this.rear = (this.rear + 1 + this.capacity) % this.capacity
  return true
}

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteFront = function() {
  if (this.isEmpty()) {
    return false
  }
  this.front = (this.front + 1) % this.capacity
  return true
}

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteLast = function() {
  if (this.isEmpty()) {
    return false
  }
  this.rear = (this.rear - 1 + this.capacity) % this.capacity
  return true
}

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

/**
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
  if (this.isEmpty()) {
    return -1
  }
  return this.elements[(this.rear - 1 + this.capacity) % this.capacity]
}

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isEmpty = function() {
  return this.rear == this.front
}

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isFull = function() {
  return (this.rear + 1) % this.capacity == this.front
}

复杂度分析:

  • 时间复杂度:初始化和每项操作的时间复杂度均为 O(1)。
  • 空间复杂度:O(k),其中 k 为给定的队列元素数目。

参考