跳转至

641.Design Circular Deque

Tags: Medium Queue Design

Links: https://leetcode.com/problems/design-circular-deque/


Design your implementation of the circular double-ended queue (deque).

Your implementation should support following operations:

  • MyCircularDeque(k): Constructor, set the size of the deque to be k.
  • insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
  • insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
  • deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
  • deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
  • getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
  • getRear(): Gets the last item from Deque. If the deque is empty, return -1.
  • isEmpty(): Checks whether Deque is empty or not.
  • isFull(): Checks whether Deque is full or not.

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);            // return true
circularDeque.insertLast(2);            // return true
circularDeque.insertFront(3);           // return true
circularDeque.insertFront(4);           // return false, the queue is full
circularDeque.getRear();            // return 2
circularDeque.isFull();             // return true
circularDeque.deleteLast();         // return true
circularDeque.insertFront(4);           // return true
circularDeque.getFront();           // return 4

Note:

  • All values will be in the range of [0, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in Deque library.

和LeetCode 622: 设计你的循环队列实现基本思路是一致的,双指针指定前后端,需要特别注意插入元素时start == end的情况。

class MyCircularDeque {
    vector<int> num;
    int n;
    int start, end, len;
public:
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k) {
        std::ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        num.resize(k);
        n = k;
        start = end = len = 0;
    }

    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if (len == n) return false;

        if (!len) {
            num[start] = value;
            ++len;
        }
        else {
            start = (start - 1 + n) % n;
            num[start] = value;
            ++len;
        }

        return true;
    }

    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
        if (len == n) return false;

        if (!len) {
            num[end] = value;
            ++len;
        }
        else {
            end = (end + 1) % n;
            num[end] = value;
            ++len;
        }

        return true;
    }

    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if (len == 0) return false;

        if (start == end) --len;
        else {
            start = (start + 1) % n;
            --len;
        }

        return true;
    }

    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if (len == 0) return false;

        if (start == end) --len;
        else {
            end = (end - 1 + n) % n;
            --len;
        }

        return true;
    }

    /** Get the front item from the deque. */
    int getFront() {
        if (len == 0) return -1;
        return num[start];
    }

    /** Get the last item from the deque. */
    int getRear() {
        if (len == 0) return -1;
        return num[end];
    }

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

    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        return len == n;
    }
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */