1. 程式人生 > >python leetcode 284. Peeking Iterator

python leetcode 284. Peeking Iterator

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