Design your implementation of the circular double-ended queue (deque).
Implement the MyCircularDeque class:
MyCircularDeque(int k) Initializes the deque with a maximum size of k.
boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.
int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.
int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.
boolean isEmpty() Returns true if the deque is empty, or false otherwise.
boolean isFull() Returns true if the deque is full, or false otherwise.
Example 1:
Input
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 2, true, true, true, 4]
Explanation
MyCircularDeque myCircularDeque = new MyCircularDeque(3);
myCircularDeque.insertLast(1); // return True
myCircularDeque.insertLast(2); // return True
myCircularDeque.insertFront(3); // return True
myCircularDeque.insertFront(4); // return False, the queue is full.
myCircularDeque.getRear(); // return 2
myCircularDeque.isFull(); // return True
myCircularDeque.deleteLast(); // return True
myCircularDeque.insertFront(4); // return True
myCircularDeque.getFront(); // return 4
Constraints:
1 <= k <= 1000
0 <= value <= 1000
At most 2000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.
設計迴圈雙端佇列。
設計實現雙端佇列。實現 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 。
思路
這道題思路類似 622 題,無非是把普通的 queue 變成 dequeue。做這道題之前可以先做一些相關題目。
複雜度
時間O(n)
空間O(n)
程式碼
Java實現
class Node {
int value;
Node prev;
Node next;
public Node(int value) {
this.value = value;
}
}
class MyCircularDeque {
int size;
int k;
Node head;
Node tail;
public MyCircularDeque(int k) {
this.size = 0;
this.k = k;
head = new Node(-1);
tail = new Node(-1);
head.next = tail;
tail.prev = head;
}
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
Node newNode = new Node(value);
newNode.next = head.next;
newNode.prev = head;
head.next.prev = newNode;
head.next = newNode;
size++;
return true;
}
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
Node newNode = new Node(value);
newNode.next = tail;
newNode.prev = tail.prev;
tail.prev.next = newNode;
tail.prev = newNode;
size++;
return true;
}
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
head.next.next.prev = head;
head.next = head.next.next;
size--;
return true;
}
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
tail.prev.prev.next = tail;
tail.prev = tail.prev.prev;
size--;
return true;
}
public int getFront() {
return head.next.value;
}
public int getRear() {
return tail.prev.value;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == k;
}
}
/**
* 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();
*/
相關題目
146. LRU Cache
460. LFU Cache
622. Design Circular Queue
641. Design Circular Deque