https://www.cnblogs.com/grandyang/p/9899490.html
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.
X.
https://leetcode.com/problems/design-circular-deque/discuss/149371/Java-doubly-LinkedList-solution-very-straightforward
traightforward solution, by using doubly linkedlist node
class MyCircularDeque {
int size;
int k;
DoubleListNode head;
DoubleListNode tail;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
head = new DoubleListNode(-1);
tail = new DoubleListNode(-1);
head.pre = tail;
tail.next = head;
this.k = k;
this.size = 0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (size == k)
return false;
DoubleListNode node = new DoubleListNode(value);
node.next = head;
node.pre = head.pre;
head.pre.next = node;
head.pre = node;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (size == k)
return false;
DoubleListNode node = new DoubleListNode(value);
node.next = tail.next;
tail.next.pre = node;
tail.next = node;
node.pre = tail;
size++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (size == 0)
return false;
head.pre.pre.next = head;
head.pre = head.pre.pre;
size--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (size == 0)
return false;
tail.next.next.pre = tail;
tail.next = tail.next.next;
size--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return head.pre.val;
}
/** Get the last item from the deque. */
public int getRear() {
return tail.next.val;
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return size == 0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return size == k;
}
}
class DoubleListNode {
DoubleListNode pre;
DoubleListNode next;
int val;
public DoubleListNode(int val) {
this.val = val;
}
}
https://leetcode.com/problems/design-circular-deque/discuss/180912/Super-consice-Java-Solution-!!! int[] a;
int front, rear, cap;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
a = new int[k + 1];
front = 0;
rear = 0;
cap = k + 1;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull()) return false;
a[front] = value;
front = (front + 1) % cap;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull()) return false;
rear = (rear - 1 + cap) % cap;
a[rear] = value;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(isEmpty()) return false;
front = (front - 1 + cap) % cap;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(isEmpty()) return false;
rear = (rear + 1) % cap;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return isEmpty() ? -1 : a[(front - 1 + cap) % cap];
}
/** Get the last item from the deque. */
public int getRear() {
return isEmpty() ? -1 : a[rear];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return front == rear;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return (front + 1) % cap == rear;
}
https://www.geeksforgeeks.org/implementation-deque-using-circular-array/
这道题让我们设计一个环形双向队列,由于之前刚做过一道Design Circular Queue,那道设计一个环形队列,其实跟这道题非常的类似,环形双向队列在环形队列的基础上多了几个函数而已,其实本质并没有啥区别,那么之前那道题的解法一改吧改吧也能用在这道题上