> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Flatten 2D Vector Python Solution with Tests

> Tested Python solution for LeetCode 251 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 251, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), Iterator. [View on LeetCode](https://leetcode.com/problems/flatten-2d-vector/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 251   # by problem number
lcpy gen -s flatten_2d_vector   # by problem name
```

## Problem

Design an iterator to flatten a 2D vector. It should support the `next` and `hasNext` operations.

Implement the `Vector2D` class:

* `Vector2D(int[][] vec)` initializes the object with the 2D vector `vec`.
* `next()` returns the next element from the 2D vector and moves the pointer one step forward. You may assume that all the calls to `next` are valid.
* `hasNext()` returns `true` if there are still some elements in the vector, and `false` otherwise.

### Examples

```
Input
["Vector2D", "next", "next", "next", "hasNext", "hasNext", "next", "hasNext"]
[[[[1, 2], [3], [4]]], [], [], [], [], [], [], []]
Output
[null, 1, 2, 3, true, true, 4, false]

Explanation
Vector2D vector2D = new Vector2D([[1, 2], [3], [4]]);
vector2D.next();    // return 1
vector2D.next();    // return 2
vector2D.next();    // return 3
vector2D.hasNext(); // return True
vector2D.hasNext(); // return True
vector2D.next();    // return 4
vector2D.hasNext(); // return False
```

### Constraints

* `0 <= vec.length <= 200`
* `0 <= vec[i].length <= 500`
* `-500 <= vec[i][j] <= 500`
* At most `10^5` calls will be made to `next` and `hasNext`.

**Follow up:** As an added challenge, try to code it using only iterators in C++ or iterators in Java.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flatten_2d_vector/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flatten_2d_vector/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Vector2D:
    # Time: O(1) amortized per call
    # Space: O(1)
    def __init__(self, vec: list[list[int]]) -> None:
        self.vec = vec
        self.row = 0
        self.col = 0

    def _skip_empty_rows(self) -> None:
        while self.row < len(self.vec) and self.col == len(self.vec[self.row]):
            self.row += 1
            self.col = 0

    def next(self) -> int:
        self._skip_empty_rows()
        value = self.vec[self.row][self.col]
        self.col += 1
        return value

    def has_next(self) -> bool:
        self._skip_empty_rows()
        return self.row < len(self.vec)
```

## Complexity

| Time | Space |
| - | - |
| O(1) amortized per call | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.