Class PeekingIterator

java.lang.Object
g0201_0300.s0284_peeking_iterator.PeekingIterator
All Implemented Interfaces:
Iterator<Integer>

public class PeekingIterator extends Object implements Iterator<Integer>
284 - Peeking Iterator.<p>Medium</p> <p>Design an iterator that supports the <code>peek</code> operation on an existing iterator in addition to the <code>hasNext</code> and the <code>next</code> operations.</p> <p>Implement the <code>PeekingIterator</code> class:</p> <ul> <li><code>PeekingIterator(Iterator<int> nums)</code> Initializes the object with the given integer iterator <code>iterator</code>.</li> <li><code>int next()</code> Returns the next element in the array and moves the pointer to the next element.</li> <li><code>boolean hasNext()</code> Returns <code>true</code> if there are still elements in the array.</li> <li><code>int peek()</code> Returns the next element in the array <strong>without</strong> moving the pointer.</li> </ul> <p><strong>Note:</strong> Each language may have a different implementation of the constructor and <code>Iterator</code>, but they all support the <code>int next()</code> and <code>boolean hasNext()</code> functions.</p> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <pre><code> [&quot;PeekingIterator&quot;, &quot;next&quot;, &quot;peek&quot;, &quot;next&quot;, &quot;next&quot;, &quot;hasNext&quot;] [[[1, 2, 3]], [], [], [], [], []] </code></pre> <p><strong>Output:</strong> [null, 1, 2, 2, 3, false]</p> <p><strong>Explanation:</strong></p> <pre><code> PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3] peekingIterator.next(); // return 1, the pointer moves to the next element [1,2,3]. peekingIterator.peek(); // return 2, the pointer does not move [1,2,3]. peekingIterator.next(); // return 2, the pointer moves to the next element [1,2,3] peekingIterator.next(); // return 3, the pointer moves to the next element [1,2,3] peekingIterator.hasNext(); // return False </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>1 <= nums[i] <= 1000</code></li> <li>All the calls to <code>next</code> and <code>peek</code> are valid.</li> <li>At most <code>1000</code> calls will be made to <code>next</code>, <code>hasNext</code>, and <code>peek</code>.</li> </ul> <p><strong>Follow up:</strong> How would you extend your design to be generic and work with all types, not just integer?</p>