-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPeekingIterator.py
More file actions
65 lines (57 loc) · 1.81 KB
/
PeekingIterator.py
File metadata and controls
65 lines (57 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 284. Peeking Iterator
# Below is the interface for Iterator, which is already defined for you.
class Iterator:
def __init__(self, nums):
"""
Initializes an iterator object to the beginning of a list.
:type nums: List[int]
"""
self.nums = nums
self.index = -1
def hasNext(self):
"""
Returns true if the iteration has more elements.
:rtype: bool
"""
return self.index < len(self.nums)
def next(self):
"""
Returns the next element in the iteration.
:rtype: int
"""
self.index += 1
return self.nums[self.index]
class PeekingIterator:
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
self.iter = iterator
self.flag = False # 特殊标识符,当第一次调用peek时,将它置为True,表示指针指向下一个节点,当调用next的时候,
# 我们知道可以不调用next,而直接返回值。当它为False的时候,next函数照常操作。
self.curr = None
def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
self.curr = self.next()
self.flag = True # 表明指针以指向下一个节点,调用next的时候可以不需要移动指针
return self.curr
def next(self):
"""
:rtype: int
"""
if not self.flag:
self.curr = self.iter.next()
self.flag = False
return self.curr
def hasNext(self):
"""
:rtype: bool
"""
if self.flag:
return True
else:
return self.iter.hasNext()