Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 943 Bytes

251. Flatten-2D-Vector.md

File metadata and controls

58 lines (41 loc) · 943 Bytes

Description

Implement an iterator to flatten a 2d vector.

Example:

Input: 2d vector =
[
  [1,2],
  [3],
  [4,5,6]
]
Output: [1,2,3,4,5,6]
Explanation: By calling next repeatedly until hasNext returns false, 
             the order of elements returned by next should be: [1,2,3,4,5,6].

太简单了……

python solution

class Vector2D(object):

    def __init__(self, vec2d):
        """
        Initialize your data structure here.
        :type vec2d: List[List[int]]
        """
        self.arr = []
        for arr in vec2d:
            self.arr += arr

    def next(self):
        """
        :rtype: int
        """
        return self.arr.pop(0)

    def hasNext(self):
        """
        :rtype: bool
        """
        return len(self.arr) > 0

# Your Vector2D object will be instantiated and called as such:
# i, v = Vector2D(vec2d), []
# while i.hasNext(): v.append(i.next())