Description

Implement an iterator to flatten a 2d vector.

For example, Given 2d vector =

[
  [1,2],
  [3],
  [4,5,6]
]

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

Solution

维护一对坐标(x, y)记录遍历到当前的坐标点
hasNext: x <= m || y <= n
next: if (x <= m) x++; else if (y <= n) y++; x = 0;

results matching ""

    No results matching ""