Class MyCircularDeque
- java.lang.Object
-
- g0601_0700.s0641_design_circular_deque.MyCircularDeque
-
public class MyCircularDeque extends Object
641 - Design Circular Deque.Medium
Design your implementation of the circular double-ended queue (deque).
Implement the
MyCircularDequeclass:MyCircularDeque(int k)Initializes the deque with a maximum size ofk.boolean insertFront()Adds an item at the front of Deque. Returnstrueif the operation is successful, orfalseotherwise.boolean insertLast()Adds an item at the rear of Deque. Returnstrueif the operation is successful, orfalseotherwise.boolean deleteFront()Deletes an item from the front of Deque. Returnstrueif the operation is successful, orfalseotherwise.boolean deleteLast()Deletes an item from the rear of Deque. Returnstrueif the operation is successful, orfalseotherwise.int getFront()Returns the front item from the Deque. Returns-1if the deque is empty.int getRear()Returns the last item from Deque. Returns-1if the deque is empty.boolean isEmpty()Returnstrueif the deque is empty, orfalseotherwise.boolean isFull()Returnstrueif the deque is full, orfalseotherwise.
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 <= 10000 <= value <= 1000- At most
2000calls will be made toinsertFront,insertLast,deleteFront,deleteLast,getFront,getRear,isEmpty,isFull.
-
-
Constructor Summary
Constructors Constructor Description MyCircularDeque(int k)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleandeleteFront()booleandeleteLast()intgetFront()intgetRear()booleaninsertFront(int value)booleaninsertLast(int value)booleanisEmpty()booleanisFull()
-
-
-
Method Detail
-
insertFront
public boolean insertFront(int value)
-
insertLast
public boolean insertLast(int value)
-
deleteFront
public boolean deleteFront()
-
deleteLast
public boolean deleteLast()
-
getFront
public int getFront()
-
getRear
public int getRear()
-
isEmpty
public boolean isEmpty()
-
isFull
public boolean isFull()
-
-