forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzigzag-iterator.py
More file actions
34 lines (27 loc) · 747 Bytes
/
zigzag-iterator.py
File metadata and controls
34 lines (27 loc) · 747 Bytes
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
# Time: O(n)
# Space: O(k)
import collections
class ZigzagIterator(object):
def __init__(self, v1, v2):
"""
Initialize your q structure here.
:type v1: List[int]
:type v2: List[int]
"""
self.q = collections.deque([(len(v), iter(v)) for v in (v1, v2) if v])
def next(self):
"""
:rtype: int
"""
len, iter = self.q.popleft()
if len > 1:
self.q.append((len-1, iter))
return next(iter)
def hasNext(self):
"""
:rtype: bool
"""
return bool(self.q)
# Your ZigzagIterator object will be instantiated and called as such:
# i, v = ZigzagIterator(v1, v2), []
# while i.hasNext(): v.append(i.next())