[数据结构、循环] 641. 设计循环双端队列

113 阅读3分钟

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

每日刷题 2022.08.15

题目

  • 设计实现双端队列。
  • 实现 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 。

示例

输入 ["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]

解释:

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

解题思路

  • 最容易想到的解法:就是直接使用数组来代替双端队列,直接使用js中的shift\pop\unshift\push方法。(这也算是一种面试官觉得直接让你“回家等通知”的写法,因为根本就没有体现出你设计的方面,而是直接调用了函数解决了问题)

循环双端队列

  • 那么什么是循环双端队列呢?也就是既可以从队列的头部和尾部插入数据,也可以删除数据。和普通的队列的区别存在于:普通的队列只能在头部删除、尾部插入,具有先进先出的特点。
  • 循环双端队列需要申明一个头节点(记为:this.he)和尾节点(记为:this.ta),将双端队列的容量记为this.cap = k + 1
    • 为什么是k + 1而不是k呢?因为我们需要将双端队列为,这两种情况区分开.
    • 首尾相等的时候,表示循环队列为空this.he === this.ta
    • 首尾差1的时候,表示循环队列满 (this.ta + 1) % this.cap === this.he,需要多一个格子来进行判断双端队列🈵️。
  • 由于是循环♻️的,因此需要取模计算.
  • 首指针:指向的就是当前整个队列的头节点,如果需要从头部插入数据,就需要先将this.he--,再插入数据
  • 尾指针:指向的是当前整个队列的尾节点的下一个节点,因此在尾部插入节点,需要先插入数据,再将this.ta++
  • 注意:⚠️需要注意头节点和尾节点的处理顺序,并不是一致的。

AC代码

/**
 * @param {number} k
 */
var MyCircularDeque = function(k) {
  // 需要使用双指针,循环,和一个数组,大小为k
  // 那么开始节点和尾部节点为空,两个指针指向一个节点的时候
  // 满的,两个指针相差为1
  this.he = 0;
  this.ta = 0;
  this.arr = new Array(k + 1).fill(0);
  this.cap = k + 1;
};

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

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

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

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

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

/**
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
  if(this.isEmpty()) return -1;
  // console.log(this.arr, this.ta, this.he)
  return this.arr[(this.ta + this.cap - 1) % this.cap];
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isEmpty = function() {
  if(this.he === this.ta) return true;
  return false;
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isFull = function() {
  // console.log(this.ta , this.he)
  if((this.ta + 1) % this.cap === this.he) return true;
  return false;
};