[LeetCode] 641、设计循环双端队列

题目描述

设计循环双端队列。

解题思路

这道题的前导问题是「力扣」第 622 题:设计循环队列。在实现上几乎是一模一样的,要注意的地方有:

  • 数组实现循环双端队列

    在数组里的操作,我们参考“动态数组”的实现来完成,主要是为了让每一步的操作复杂度都最低。只不过不要求我们实现动态扩容与缩容。本题我们需要注意的地方有:

    • 定义循环变量 frontrear 。一直保持这个定义,到底是先赋值还是先移动指针就很容易想清楚了。

      • front:指向队列头部第1个有效数据的位置;
      • rear:指向队列尾部(即最后1个有效数据)的下一个位置,即下一个从队尾入队元素的位置。

      (说明:这个定义是依据“动态数组”的定义模仿而来的)

    • 为了避免“队列为空”和“队列为满”的判别条件冲突,我们有意浪费了一个位置。

      浪费一个位置是指:循环数组中任何时刻一定至少有一个位置不存放有效元素。

      • 判别队列为空的条件是:front == rear;
      • 判别队列为满的条件是:(rear + 1) % capacity == front;。可以这样理解:当 rear 循环到数组的前面,要从后面追上 front,还差一格的时候,即判定队列为满。
    • 因为有循环的出现,要特别注意处理数组下标可能越界的情况。

      • 指针后移的时候,索引+1,所以要注意取模。
      • 指针前移的时候,为了防止越界,需要先加上数组的长度,然后再对数组长度取模。

参考代码

class MyCircularDeque {

private:
    vector<int> arr;
    int front;
    int rear;
    int capacity;

public:
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k) {
        capacity = k + 1;
        arr.assign(capacity, 0);  // 要多分配一个位置的空间才行

        front = 0;
        rear = 0;
    }

    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if (isFull()) {
            return false;
        }
        front = (front - 1 + capacity) % capacity;
        arr[front] = value;
        return true;
    }

    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
        if (isFull()) {
            return false;
        }
        arr[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }

    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if (isEmpty()) {
            return false;
        }
        // front 被设计在数组的开头,所以是 +1
        front = (front + 1) % capacity;
        return true;
    }

    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if (isEmpty()) {
            return false;
        }
        // rear被设计在数组最后一个元素的下一个位置,所以是 -1
        rear = (rear - 1 + capacity) % capacity;  // 注:循环队列的长度为 (rear - front + capacity) % capacity
        return true;
    }

    /** Get the front item from the deque. */
    int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return arr[front];
    }

    /** Get the last item from the deque. */
    int getRear() {
        if (isEmpty()) {
            return -1;
        }
        // 当rear为0时,防止数组越界
        return arr[(rear - 1 + capacity) % capacity]; 
    }

    /** Checks whether the circular deque is empty or not. */
    bool isEmpty() {
        return front == rear;
    }

    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        // 注意:这个设计是非常经典的做法
        return (rear + 1) % capacity == front;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值