Class MyCircularDeque

java.lang.Object
g0601_0700.s0641_design_circular_deque.MyCircularDeque

public class MyCircularDeque extends Object
641 - Design Circular Deque.<p>Medium</p> <p>Design your implementation of the circular double-ended queue (deque).</p> <p>Implement the <code>MyCircularDeque</code> class:</p> <ul> <li><code>MyCircularDeque(int k)</code> Initializes the deque with a maximum size of <code>k</code>.</li> <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> <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> <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> <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> <li><code>int getFront()</code> Returns the front item from the Deque. Returns <code>-1</code> if the deque is empty.</li> <li><code>int getRear()</code> Returns the last item from Deque. Returns <code>-1</code> if the deque is empty.</li> <li><code>boolean isEmpty()</code> Returns <code>true</code> if the deque is empty, or <code>false</code> otherwise.</li> <li><code>boolean isFull()</code> Returns <code>true</code> if the deque is full, or <code>false</code> otherwise.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;MyCircularDeque&rdquo;, &ldquo;insertLast&rdquo;, &ldquo;insertLast&rdquo;, &ldquo;insertFront&rdquo;, &ldquo;insertFront&rdquo;, &ldquo;getRear&rdquo;, &ldquo;isFull&rdquo;, &ldquo;deleteLast&rdquo;, &ldquo;insertFront&rdquo;, &ldquo;getFront&rdquo;]</p> <p>[[3], [1], [2], [3], [4], [], [], [], [4], []]</p> <p><strong>Output:</strong> [null, true, true, true, false, 2, true, true, true, 4]</p> <p><strong>Explanation:</strong></p> <p>MyCircularDeque myCircularDeque = new MyCircularDeque(3);</p> <p>myCircularDeque.insertLast(1); // return True</p> <p>myCircularDeque.insertLast(2); // return True</p> <p>myCircularDeque.insertFront(3); // return True</p> <p>myCircularDeque.insertFront(4); // return False, the queue is full.</p> <p>myCircularDeque.getRear(); // return 2</p> <p>myCircularDeque.isFull(); // return True</p> <p>myCircularDeque.deleteLast(); // return True</p> <p>myCircularDeque.insertFront(4); // return True</p> <p>myCircularDeque.getFront(); // return 4</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= k <= 1000</code></li> <li><code>0 <= value <= 1000</code></li> <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> </ul>
  • Constructor Details

    • MyCircularDeque

      public MyCircularDeque(int k)
  • Method Details

    • 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()