[
design
]
Leetcode 0284. Peeking Iterator
https://leetcode.com/problems/peeking-iterator
This is more design problem, not algorithmic one in my opinion. You already given implemented class iterator
, which you can understand as list, but it is not list. The goal is to implement so-called peeking iterator and to do this we need to be one step ahead: let us create self.buffer
variable, which will keep next value from our iterator.
- When we call
peek
function, we just return value from our buffer. - When we call
next
function, we write buffer variable totmp
, then we update our buffer: if we have next element, we go to the next element, if we do not have it we make it equal toNone
. - Finally,
hasNext
function now is just checking if buffer is empty or not.
Complexity: it is O(1)
for all operations, if it was O(1)
for original iterator
class.
class PeekingIterator:
def __init__(self, iterator):
self.iterator = iterator
self.buffer = self.iterator.next() if self.iterator.hasNext() else None
def peek(self):
return self.buffer
def next(self):
tmp = self.buffer
self.buffer = self.iterator.next() if self.iterator.hasNext() else None
return tmp
def hasNext(self):
return self.buffer != None
If you like the solution, you can upvote it on leetcode discussion section: Problem 0284