-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zigzag Iterator.py
41 lines (34 loc) · 1.03 KB
/
Zigzag 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
"""
https://leetcode.com/problems/zigzag-iterator/
评论: 自己在形成v3的时候写的东西有点乱。可以用一个变量Lcommon来表达v1与v2的common length部分。
"""
class ZigzagIterator(object):
def __init__(self, v1, v2):
self.v1 = v1
self.v2 = v2
self.v3 = []
if (len(v1)<=len(v2)):
for index,value in enumerate(v1):
self.v3.append(value)
self.v3.append(v2[index])
for value in self.v2[len(v1)::]:
self.v3.append(value)
else:
for index,value in enumerate(v2):
self.v3.append(v1[index])
self.v3.append(value)
for value in self.v1[len(v2)::]:
self.v3.append(value)
self.n = 0
def next(self):
"""
:rtype: int
"""
x = self.v3[self.n]
self.n += 1
return x
def hasNext(self):
"""
:rtype: bool
"""
return not (self.n == len(self.v3))