|
| 1 | +<h2><a href="https://leetcode.com/problems/design-circular-deque">Design Circular Deque</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Design your implementation of the circular double-ended queue (deque).</p> |
| 2 | + |
| 3 | +<p>Implement the <code>MyCircularDeque</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>MyCircularDeque(int k)</code> Initializes the deque with a maximum size of <code>k</code>.</li> |
| 7 | + <li><code>boolean insertFront()</code> Adds an item at the front of Deque. Returns <code>true</code> if the operation is successful, or <code>false</code> otherwise.</li> |
| 8 | + <li><code>boolean insertLast()</code> Adds an item at the rear of Deque. Returns <code>true</code> if the operation is successful, or <code>false</code> otherwise.</li> |
| 9 | + <li><code>boolean deleteFront()</code> Deletes an item from the front of Deque. Returns <code>true</code> if the operation is successful, or <code>false</code> otherwise.</li> |
| 10 | + <li><code>boolean deleteLast()</code> Deletes an item from the rear of Deque. Returns <code>true</code> if the operation is successful, or <code>false</code> otherwise.</li> |
| 11 | + <li><code>int getFront()</code> Returns the front item from the Deque. Returns <code>-1</code> if the deque is empty.</li> |
| 12 | + <li><code>int getRear()</code> Returns the last item from Deque. Returns <code>-1</code> if the deque is empty.</li> |
| 13 | + <li><code>boolean isEmpty()</code> Returns <code>true</code> if the deque is empty, or <code>false</code> otherwise.</li> |
| 14 | + <li><code>boolean isFull()</code> Returns <code>true</code> if the deque is full, or <code>false</code> otherwise.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong class="example">Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input</strong> |
| 22 | +["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] |
| 23 | +[[3], [1], [2], [3], [4], [], [], [], [4], []] |
| 24 | +<strong>Output</strong> |
| 25 | +[null, true, true, true, false, 2, true, true, true, 4] |
| 26 | + |
| 27 | +<strong>Explanation</strong> |
| 28 | +MyCircularDeque myCircularDeque = new MyCircularDeque(3); |
| 29 | +myCircularDeque.insertLast(1); // return True |
| 30 | +myCircularDeque.insertLast(2); // return True |
| 31 | +myCircularDeque.insertFront(3); // return True |
| 32 | +myCircularDeque.insertFront(4); // return False, the queue is full. |
| 33 | +myCircularDeque.getRear(); // return 2 |
| 34 | +myCircularDeque.isFull(); // return True |
| 35 | +myCircularDeque.deleteLast(); // return True |
| 36 | +myCircularDeque.insertFront(4); // return True |
| 37 | +myCircularDeque.getFront(); // return 4 |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= k <= 1000</code></li> |
| 45 | + <li><code>0 <= value <= 1000</code></li> |
| 46 | + <li>At most <code>2000</code> calls will be made to <code>insertFront</code>, <code>insertLast</code>, <code>deleteFront</code>, <code>deleteLast</code>, <code>getFront</code>, <code>getRear</code>, <code>isEmpty</code>, <code>isFull</code>.</li> |
| 47 | +</ul> |
0 commit comments