-
Notifications
You must be signed in to change notification settings - Fork 43
/
peeking-iterator.py
143 lines (111 loc) · 3.85 KB
/
peeking-iterator.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
284. Peeking Iterator
Medium
Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the next operations.
Implement the PeekingIterator class:
PeekingIterator(Iterator<int> nums) Initializes the object with the given integer iterator iterator.
int next() Returns the next element in the array and moves the pointer to the next element.
boolean hasNext() Returns true if there are still elements in the array.
int peek() Returns the next element in the array without moving the pointer.
Note: Each language may have a different implementation of the constructor and Iterator, but they all support the int next() and boolean hasNext() functions.
Example 1:
Input
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 2, 2, 3, false]
Explanation
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next(); // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek(); // return 2, the pointer does not move [1,2,3].
peekingIterator.next(); // return 2, the pointer moves to the next element [1,2,3]
peekingIterator.next(); // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
All the calls to next and peek are valid.
At most 1000 calls will be made to next, hasNext, and peek.
Follow up: How would you extend your design to be generic and work with all types, not just integer?
"""
# V0
# V1
# https://leetcode.com/problems/peeking-iterator/discuss/123811/Python-solution
class PeekingIterator(object):
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
self.next_val = None
self.iterator = iterator
if self.iterator.hasNext():
self.next_val = iterator.next()
def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
return self.next_val
def next(self):
"""
:rtype: int
"""
cur_val = self.next_val
if self.iterator.hasNext():
self.next_val = self.iterator.next()
else:
self.next_val = None
return cur_val
def hasNext(self):
"""
:rtype: bool
"""
return self.next_val is not None
# V1'
# https://leetcode.com/problems/peeking-iterator/discuss/72626/Simple-Python-Solution
class PeekingIterator(object):
def __init__(self, iterator):
self.iter = iterator
self.temp = self.iter.next() if self.iter.hasNext() else None
def peek(self):
return self.temp
def next(self):
ret = self.temp
self.temp = self.iter.next() if self.iter.hasNext() else None
return ret
def hasNext(self):
return self.temp is not None
# V1''
# https://leetcode.com/problems/peeking-iterator/discuss/729795/Python-Solution
class PeekingIterator:
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
self.iter = iterator
self.cache = None
def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
if self.cache is None:
self.cache = self.iter.next()
return self.cache
def next(self):
"""
:rtype: int
"""
if self.cache is not None:
temp = self.cache
self.cache = None
return temp
return self.iter.next()
def hasNext(self):
"""
:rtype: bool
"""
return self.cache is not None or self.iter.hasNext()
# V2