1
/*
2
* Below is the interface for Iterator, which is already defined for you.
3
* **DO NOT** modify the interface for Iterator.
4
*
5
* class Iterator {
6
* struct Data;
7
* Data* data;
8
* public:
9
* Iterator(const vector<int>& nums);
10
* Iterator(const Iterator& iter);
11
*
12
* // Returns the next element in the iteration.
13
* int next();
14
*
15
* // Returns true if the iteration has more elements.
16
* bool hasNext() const;
17
* };
18
*/
19

20
class PeekingIterator : public Iterator {
21
public:
22
int _nextVal;
23
bool _hasNext;
24
PeekingIterator(const vector<int> &nums) : Iterator(nums) {
25
// Initialize any member here.
26
// **DO NOT** save a copy of nums and manipulate it directly.
27
// You should only use the Iterator interface methods.
28
_nextVal = 0;
29
_hasNext = Iterator::hasNext();
30
if (_hasNext) _nextVal = Iterator::next();
31
}
32

33
// Returns the next element in the iteration without advancing the iterator.
34
int peek() {
35
return (_nextVal);
36
}
37

38
// hasNext() and next() should behave the same as in the Iterator interface.
39
// Override them if needed.
40
int next() {
41
int tmp = _nextVal;
42

43
_hasNext = Iterator::hasNext();
44
if (_hasNext) _nextVal = Iterator::next();
45
return (tmp);
46
}
47

48
bool hasNext() const {
49
return (_hasNext);
50
}
51
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0