Skip to content

Commit d648361

Browse files
committed
New Problem "Peeking Iterator"
1 parent c022965 commit d648361

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LeetCode
1414
|290|[Word Pattern](https://leetcode.com/problems/word-pattern/) | [C++](./algorithms/cpp/wordPattern/WordPattern.cpp)|Easy|
1515
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C++](./algorithms/cpp/findTheDuplicateNumber/findTheDuplicateNumber.cpp)|Hard|
1616
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/) ♥ | [Java](./algorithms/java/src/inorderSuccessorInBST/inorderSuccessorInBST.java)|Medium|
17+
|284|[Peeking Iterator](https://leetcode.com/problems/peeking-iterator/) | [C++](./algorithms/cpp/peekingIterator/PeekingIterator.cpp)|Medium|
1718
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./algorithms/cpp/moveZeroes/moveZeroes.cpp)|Easy|
1819
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [C++](./algorithms/cpp/perfectSquares/PerfectSquares.cpp)|Medium|
1920
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)| [C++](./algorithms/cpp/firstBadVersion/FirstBadVersion.cpp), [Java](./algorithms/java/src/firstBadVersion/firstBadVersion.java)|Easy|

Diff for: algorithms/cpp/peekingIterator/PeekingIterator.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)