Two-Dimensional List Iterator
Implement an iterator of two-dimensional list of integers lists
.
next()
polls the next element in the iterator, iterating over each row inlists
hasnext()
which returns whether the next element exists
For example:
it = TwoDimensionalIterator([[1, 2],[3, 4],[5]])
it.next() == 1
it.next() == 2
it.next() == 3
it.hasnext() == True
it.next() == 4
it.next() == 5
it.hasnext() == False
Constraints
n ≤ 100,000
wheren
is the number of calls tonext
andhasnext
Notes
- Your iterator should use \(\mathcal{O}(1)\) extra space.
https://binarysearch.com/problems/Two-Dimensional-List-Iterator
Examples
Example 1
Input
- methods =
['constructor', 'next', 'next', 'next', 'hasnext', 'next', 'next', 'hasnext']
- arguments =
[[[[1, 2], [3, 4], [5]]], [], [], [], [], [], [], []]
Output
- answer =
[None, 1, 2, 3, True, 4, 5, False]
Leave a comment