Flatten 2D Vector
Difficulty: Medium
Category: DSA
Topics: Array, Two Pointers, Design, Iterator
Asked at: Airbnb
You are given a 2D vector `vec`, represented as a list of lists of integers. Design an iterator that flattens the 2D vector and iterates through all the elements in row-major order (i.e., from left to right and top to bottom).
Implement the `Vector2D` class:
- `Vector2D(int[][] vec)` initializes the object with the 2D vector `vec`.
- `int next()` returns the next element in the flattened vector.
- `boolean hasNext()` returns _true_ if there are still elements to be returned, or _false_ otherwise.
Your implementation should support calling `next()` and `hasNext()` multiple times and in any order.
Return the appropriate values for each operation as described above.