1
+ // Runtime: 3 ms (Top 82.50%) | Memory: 7.5 MB (Top 36.47%)
1
2
/*
2
3
* Below is the interface for Iterator, which is already defined for you.
3
4
* **DO NOT** modify the interface for Iterator.
4
5
*
5
- * class Iterator {
6
- * struct Data;
7
- * Data* data;
8
- * public:
9
- * Iterator(const vector<int>& nums);
10
- * Iterator(const Iterator& iter);
6
+ * class Iterator {
7
+ * struct Data;
8
+ * Data* data;
9
+ * public:
10
+ * Iterator(const vector<int>& nums);
11
+ * Iterator(const Iterator& iter);
11
12
*
12
- * // Returns the next element in the iteration.
13
- * int next();
13
+ * // Returns the next element in the iteration.
14
+ * int next();
14
15
*
15
- * // Returns true if the iteration has more elements.
16
- * bool hasNext() const;
17
- * };
16
+ * // Returns true if the iteration has more elements.
17
+ * bool hasNext() const;
18
+ * };
18
19
*/
19
20
20
21
class PeekingIterator : public Iterator {
21
22
public:
22
23
int _nextVal;
23
24
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.
25
+ PeekingIterator (const vector<int >& nums) : Iterator(nums) {
26
+ // Initialize any member here.
27
+ // **DO NOT** save a copy of nums and manipulate it directly.
28
+ // You should only use the Iterator interface methods.
28
29
_nextVal = 0 ;
29
30
_hasNext = Iterator::hasNext ();
30
31
if (_hasNext)
31
32
_nextVal = Iterator::next ();
32
- }
33
-
33
+ }
34
+
34
35
// Returns the next element in the iteration without advancing the iterator.
35
- int peek () {
36
+ int peek () {
36
37
return (_nextVal);
37
- }
38
-
38
+ }
39
39
40
- // hasNext() and next() should behave the same as in the Iterator interface.
41
- // Override them if needed.
42
- int next () {
40
+ // hasNext() and next() should behave the same as in the Iterator interface.
41
+ // Override them if needed.
42
+ int next () {
43
43
int tmp = _nextVal;
44
-
44
+
45
45
_hasNext = Iterator::hasNext ();
46
46
if (_hasNext)
47
47
_nextVal = Iterator::next ();
48
48
return (tmp);
49
- }
50
-
51
- bool hasNext () const {
49
+ }
50
+
51
+ bool hasNext () const {
52
52
return (_hasNext);
53
- }
53
+ }
54
54
};
0 commit comments