|
| 1 | +// Source : https://leetcode.com/problems/peeking-iterator/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2015-11-10 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Given an Iterator class interface with methods: next() and hasNext(), design and |
| 8 | + * implement a PeekingIterator that support the peek() operation -- it essentially |
| 9 | + * peek() at the element that will be returned by the next call to next(). |
| 10 | + * |
| 11 | + * Here is an example. Assume that the iterator is initialized to the beginning of the |
| 12 | + * list: [1, 2, 3]. |
| 13 | + * |
| 14 | + * Call next() gets you 1, the first element in the list. |
| 15 | + * |
| 16 | + * Now you call peek() and it returns 2, the next element. Calling next() after that |
| 17 | + * still return 2. |
| 18 | + * |
| 19 | + * You call next() the final time and it returns 3, the last element. Calling hasNext() |
| 20 | + * after that should return false. |
| 21 | + * |
| 22 | + * Think of "looking ahead". You want to cache the next element. |
| 23 | + * Is one variable sufficient? Why or why not? |
| 24 | + * Test your design with call order of peek() before next() vs next() before peek(). |
| 25 | + * For a clean implementation, check out Google's guava library source code. |
| 26 | + * |
| 27 | + * Follow up: How would you extend your design to be generic and work with all types, |
| 28 | + * not just integer? |
| 29 | + * |
| 30 | + * Credits:Special thanks to @porker2008 for adding this problem and creating all test |
| 31 | + * cases. |
| 32 | + * |
| 33 | + ***************************************************************************************/ |
| 34 | + |
| 35 | +// Below is the interface for Iterator, which is already defined for you. |
| 36 | +// **DO NOT** modify the interface for Iterator. |
| 37 | +class Iterator { |
| 38 | + struct Data; |
| 39 | + Data* data; |
| 40 | +public: |
| 41 | + Iterator(const vector<int>& nums); |
| 42 | + Iterator(const Iterator& iter); |
| 43 | + virtual ~Iterator(); |
| 44 | + // Returns the next element in the iteration. |
| 45 | + int next(); |
| 46 | + // Returns true if the iteration has more elements. |
| 47 | + bool hasNext() const; |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | +class PeekingIterator : public Iterator { |
| 52 | +private: |
| 53 | + bool m_hasNext; |
| 54 | + int m_next; |
| 55 | + |
| 56 | + void takeNext() { |
| 57 | + m_hasNext = Iterator::hasNext(); |
| 58 | + if (m_hasNext) { |
| 59 | + m_next = Iterator::next(); |
| 60 | + } |
| 61 | + } |
| 62 | +public: |
| 63 | + PeekingIterator(const vector<int>& nums) : Iterator(nums) { |
| 64 | + // Initialize any member here. |
| 65 | + // **DO NOT** save a copy of nums and manipulate it directly. |
| 66 | + // You should only use the Iterator interface methods. |
| 67 | + takeNext(); |
| 68 | + } |
| 69 | + |
| 70 | + // Returns the next element in the iteration without advancing the iterator. |
| 71 | + int peek() { |
| 72 | + return m_next; |
| 73 | + } |
| 74 | + |
| 75 | + // hasNext() and next() should behave the same as in the Iterator interface. |
| 76 | + // Override them if needed. |
| 77 | + int next() { |
| 78 | + int temp = m_next; |
| 79 | + takeNext(); |
| 80 | + return temp; |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + bool hasNext() const { |
| 85 | + return m_hasNext; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
0 commit comments