leetcode刷题记录-641. 设计循环双端队列

57 阅读2分钟

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

前言

今天的题目为中等,通过数组模拟队列能够简单解答,但是或许能够通过其他的方法实现更快的运行速度。

每日一题

今天的题目是 641. 设计循环双端队列,难度为中等

  • 设计实现双端队列。

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

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

题解

数组模拟队列

利用数组来模拟队列,用一个参数来保存下队列的最大长度,然后在队列前后新增元素的时候需要去判断当前数组的长度和最大长度,等于最大长度的话就需要返回false。

删除队列元素就需要判断当前的队列数组是否存在元素,存在就相应的删除队尾或者队首元素。

获得头部或者尾部的元素,需要判断当前的队列中是否存在元素,存在就返回队列的头部元素或者尾部元素。

队列的判空或者判满,就是根据数组的长度和题目提供的最长长度进行对比,相等就是满,数组长度为0就是空。

class MyCircularDeque {
    Arr: number[];
    maxSize: number;
    get n(){
        return this.Arr.length
    }
    constructor(k: number) {
        this.Arr = new Array<number>();
        this.maxSize = k;
    }

    insertFront(value: number): boolean {
        if (this.n < this.maxSize) {
            this.Arr.unshift(value);
            return true;
        }
        return false;
    }

    insertLast(value: number): boolean {
        if(this.n < this.maxSize) {
            this.Arr.push(value)
            return true
        }
        return false
    }

    deleteFront(): boolean {
        if (this.n > 0) {
            this.Arr.shift();
            return true;
        }
        return false;
    }

    deleteLast(): boolean {
        if (this.n > 0) {
            this.Arr.pop();
            return true;
        }
        return false;
    }

    getFront(): number {
        if (this.n > 0) {
            return this.Arr[0];
        }
        return -1;
    }

    getRear(): number {
        if (this.n > 0) {
            return this.Arr[this.n - 1];
        }
        return -1;
    }

    isEmpty(): boolean {
        return this.n === 0;
    }

    isFull(): boolean {
        return this.n === this.maxSize;
    }
}

/**
 * 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()
 */

image.png