-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path601.flatten-2d-vector.py
46 lines (40 loc) · 1.04 KB
/
601.flatten-2d-vector.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
# Tag: Iterator
# Time: O(*1)
# Space: O(N)
# Ref: Leetcode-251
# Note: -
# Design an iterator to realize the function of flattening two-dimensional vector.
#
# Example 1:
# ```
# Input:[[1,2],[3],[4,5,6]]
# Output:[1,2,3,4,5,6]
# ```
# Example 2:
# ```
# Input:[[7,9],[5]]
# Output:[7,9,5]
# ```
#
#
class Vector2D(object):
# @param vec2d {List[List[int]]}
def __init__(self, vec2d):
# Initialize your data structure here
self.stack = vec2d[::-1]
self.res = []
# @return {int} a next element
def next(self):
# Write your code here
return self.res.pop()
# @return {boolean} true if it has next element
# or false
def hasNext(self):
# Write your code here
while len(self.stack) > 0 and len(self.res) == 0:
cur = self.stack.pop()
self.res.extend(reversed(cur))
return len(self.res) > 0
# Your Vector2D object will be instantiated and called as such:
# i, v = Vector2D(vec2d), []
# while i.hasNext(): v.append(i.next())