[Leetcode]_641 Design Circular Deque

/**
 *  Index: 641
 *  Title: Design Circular Deque
 *  Author: ltree98
 **/

题意

设计一个环形队列

支持的操作

  • MyCircularDeque(k): 构造方法,设置队列长度k
  • insertFront(): 插入元素到队头,返回值表示操作是否成功
  • insertLast(): 插入元素到队尾,返回值表示操作是否成功
  • deleteFront(): 删除队头元素,返回值表示操作是否成功
  • deleteLast(): 删除队尾元素,返回值表示操作是否成功
  • getFront(): 获取队头元素,队列为空,返回-1
  • getRear(): 获取队尾元素,队列为空,返回-1
  • isEmpty(): 返回队列是否为空
  • isFull(): 返回队列是否已满

注意:

  • 所有数值范围在 [0, 1000]
  • 所有操作返回值范围在 [0, 1000]
  • 不要用内置队列

我的

思路

普通队列

虽然题目是环形队列,我还是先做了个普通队列。用一个"尾指针"来实现。

当然,复杂的地方就是头部的操作,每次头部的插入删除,都要平移一次队列。

环形队列

题目所要求的环形队列,一个头指针,一个尾指针,再加一个当前队列实际长度的变量。

只通过头尾指针移动来进行插入删除,省去了整体迁移。

实现

普通队列

class MyCircularDeque {
private:
    vector<int> buffer;
    int rear;
    int size;
        
public:
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k): buffer(k, 0), size(k), rear(-1) {
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if(!this->isFull()) {
            ++this->rear;
            for(int i = this->rear; i > 0; i--) 
                this->buffer[i] = this->buffer[i - 1];
            this->buffer[0] = value; 

            return true;
        }
        
        return false;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
        if(!this->isFull()) {
            this->buffer[++this->rear] = value;
            
            return true;
        }
        
        return false;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if(!this->isEmpty())    {
            for(int i = 0; i < this->rear; i++)
                this->buffer[i] = this->buffer[i + 1];
            this->buffer[this->rear--] = 0;
            
            return true;
        }
        return false;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if(!this->isEmpty())    {
            this->buffer[this->rear--] = 0;
            
            return true;
        }
        return false;
    }
    
    /** Get the front item from the deque. */
    int getFront() {
        if(!this->isEmpty())
            return this->buffer[0];
        
        return -1;
    }
    
    /** Get the last item from the deque. */
    int getRear() {
        if(!this->isEmpty())    {
            return this->buffer[this->rear];
        }
        
        return -1;
    }
    
    /** Checks whether the circular deque is empty or not. */
    bool isEmpty() {
        return (this->rear == -1);
    }
    
    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        return (this->rear == this->size - 1);
    }
};

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

环形队列

class MyCircularDeque {
private:
    vector<int> buffer;
    int front;
    int rear;
    int len;
    int size;
        
public:
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k): buffer(k, 0), size(k), len(0), front(k-1), rear(0) {
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if(!this->isFull()) {
            this->buffer[this->front] = value;
            this->front = this->getRealIndex(this->front - 1);
            ++this->len;
            
            return true;
        }
        return false;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
        if(!this->isFull()) {
            this->buffer[this->rear] = value;
            this->rear = this->getRealIndex(this->rear + 1);
            ++this->len;
            
            return true;
        }
        return false;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if(!this->isEmpty()) {
            this->front = this->getRealIndex(this->front + 1);
            this->buffer[this->front] = 0;
            --this->len;
            
            return true;
        }
        return false;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if(!this->isEmpty()) {
            this->rear = this->getRealIndex(this->rear - 1);
            this->buffer[this->rear] = 0;
            --this->len;
            
            return true;
        }
        return false;
    }
    
    /** Get the front item from the deque. */
    int getFront() {
        if(!this->isEmpty())    {
            return this->buffer[this->getRealIndex(this->front + 1)];
        }
        return -1;
    }
    
    /** Get the last item from the deque. */
    int getRear() {
        if(!this->isEmpty()) {
            return this->buffer[this->getRealIndex(this->rear - 1)];
        }
        return -1;
    }
    
    /** Checks whether the circular deque is empty or not. */
    bool isEmpty() {
        return (this->len == 0);
    }
    
    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        return (this->len == this->size);
    }
    
    int getRealIndex(int index) {
        return ((index + this->size) % (this->size));
    }
};

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

发布了472 篇原创文章 · 获赞 387 · 访问量 78万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览