Leetcode 284. Peeking Iterator
Given an Iterator class interface with methods: next()
and hasNext()
, design and implement a PeekingIterator that supports the peek()
operation. The peek()
method should return the next element in the iteration without advancing the iterator. The PeekingIterator
should still support the standard operations of advancing the iterator with next()
and checking if there are elements remaining using hasNext()
.
Example:
Input:
[1, 2, 3]
Output:
[null, 1, 1, 2, 2, 3, false]
Explanation:
PeekingIterator peekIterator = new PeekingIterator([1, 2, 3]);
peekIterator.next(); // return 1
peekIterator.peek(); // return 2
peekIterator.next(); // return 2
peekIterator.next(); // return 3
peekIterator.hasNext(); // return false
peek()
return when the iterator is at the end?
peek()
when no elements are left to iterate should be addressed explicitly, but typically if hasNext()
is false
, calling peek()
would likely be undefined or might throw an exception.peek()
and next()
operations should be based on their positions, not the element values.#include <vector>
// Below is the Iterator interface implementation. Do not change it.
class Iterator {
struct Data;
Data* data;
public:
Iterator(const std::vector<int>& nums);
Iterator(const Iterator& iter);
virtual ~Iterator();
// Returns the next element in the iteration.
int next();
// Returns true if the iteration has more elements.
bool hasNext() const;
};
class PeekingIterator : public Iterator {
private:
int nextElement;
bool hasNextElement;
public:
PeekingIterator(const std::vector<int>& nums) : Iterator(nums) {
hasNextElement = Iterator::hasNext();
if (hasNextElement) {
nextElement = Iterator::next();
}
}
// Returns the next element in the iteration without advancing the iterator.
int peek() {
return nextElement;
}
// Override next() to use the cached element.
int next() {
int toReturn = nextElement;
hasNextElement = Iterator::hasNext();
if (hasNextElement) {
nextElement = Iterator::next();
}
return toReturn;
}
// Override hasNext() to include the logic for the cached element.
bool hasNext() const {
return hasNextElement;
}
};
PeekingIterator
is initialized with a given list of elements. It uses the base Iterator
constructor to initialize the iterator.nextElement
. A boolean hasNextElement
is used to keep track of whether any elements are left to iterate.nextElement
without advancing the iterator.nextElement
and advances the iterator by fetching the next element into the cache.hasNextElement
to determine if there are more elements to iterate.hasNextElement
.This design ensures that all operations (peek
, next
, hasNext
) perform efficiently with constant time complexity.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?