python leetcode 284. Peeking Iterator
阿新 • • 發佈:2018-12-22
class PeekingIterator:
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
self.iterat=iterator
self.tmp = None
def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
if not self.tmp:
self.tmp=self.iterat.next()
return self.tmp
def next(self):
"""
:rtype: int
"""
if self.tmp:
a=self.tmp
self.tmp=None
return a
else:
return self.iterat.next()
def hasNext(self):
"""
:rtype: bool
"""
return self.iterat.hasNext() if not self.tmp else True