难度:Medium
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 <= 10000 <= value <= 1000insertFront,insertLast,deleteFront,deleteLast,getFront,getRear,isEmpty,isFull调用次数不大于2000次
解法思路:
1. 数组
注意事项:
1)定义指针
- front:指向队列头部第1个有效数据的位置;
 - rear:指向队列尾部(即最后1个有效数据)的 下一个位置,即下一个从队尾入队元素的位置。
 2)队列判空判满
- 判别队列为空的条件是:front == rear;
 - 判别队列为满的条件是:(rear + 1) % capacity == front;。可以这样理解,当 rear 循环到数组的前面,要从后面追上 front,还差一格的时候,判定队列为满。
 3)下标越界
- 指针后移的时候,下标 +1,要取模;
 - 指针前移的时候,为了循环到数组的末尾,需要先加上数组的长度,然后再对数组长度取模。
 2. 链表
法一:
class MyCircularDeque {
    // 数组
    // Time: O(1)
    // Space: O(k)
    private int[] elements;
    private int rear, front;
    private int capacity;
    public MyCircularDeque(int k) {
        capacity = k + 1;
        rear = front = 0;
        elements = new int[capacity];
    }
    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        }
        front = (front - 1 + capacity) % capacity;
        elements[front] = value;
        return true;
    }
    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }
        elements[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }
    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % capacity;
        return true;
    }
    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }
        rear = (rear - 1 + capacity) % capacity;
        return true;
    }
    public int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return elements[front];
    }
    public int getRear() {
        if (isEmpty()) {
            return -1;
        }
        return elements[(rear - 1 + capacity) % capacity];
    }
    public boolean isEmpty() {
        return rear == front;
    }
    public boolean isFull() {
        return (rear + 1) % capacity == front;
    }
}
/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */ 
法二:
class MyCircularDeque {
    // 链表
    // Time: O(1)
    // Space: O(k)
    private class Node {
        int val;
        Node prev, next;
        Node(int val) {
            this.val = val;
        }
    }
    private Node head, tail;
    private int capacity;
    private int size;
    public MyCircularDeque(int k) {
        capacity = k;
        size = 0;
    }
    public boolean insertFront(int value) {
        if (size == capacity) {
            return false;
        }
        Node node = new Node(value);
        if (size == 0) {
            head = tail = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
        size++;
        return true;
    }
    public boolean insertLast(int value) {
        if (size == capacity) {
            return false;
        }
        Node node = new Node(value);
        if (size == 0) {
            head = tail = node;
        } else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
        size++;
        return true;
    }
    public boolean deleteFront() {
        if (size == 0) {
            return false;
        }
        head = head.next;
        if (head != null) {
            head.prev = null;
        }
        size--;
        return true;
    }
    public boolean deleteLast() {
        if (size == 0) {
            return false;
        }
        tail = tail.prev;
        if (tail != null) {
            tail.next = null;
        }
        size--;
        return true;
    }
    public int getFront() {
        if (size == 0) {
            return -1;
        }
        return head.val;
    }
    public int getRear() {
        if (size == 0) {
            return -1;
        }
        return tail.val;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public boolean isFull() {
        return size == capacity;
    }
}
/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */ 
                



















