Tap here to switch tabs
Problem
Submissions

Problem: Zigzag Iterator

med
30 min
Try to solve the Zigzag Iterator problem.

Statement

Design an iterator class ZigzagIterator that takes two integer arrays v1 and v2 and iterates through their elements by alternating between the arrays.

The iterator must support the following methods:

  • next() returns the next integer in the alternating traversal.

  • hasNext() returns true if there is at least one remaining element to return, otherwise returns false.

When one of the arrays runs out of elements, the iterator should continue returning the remaining elements from the other array in order.

Note: For the follow up, if more than two arrays are provided, interpret the order as cyclic across the arrays, skipping any array that has no remaining elements.

Constraints:

  • 0≤0 \leq v1.length, v2.length ≤1000\leq 1000

  • 1≤1 \leq v1.length ++ v2.length ≤2000\leq 2000

  • −231≤-2^{31} \leq v1[i], v2[i] ≤231−1\leq 2^{31} - 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Zigzag Iterator

med
30 min
Try to solve the Zigzag Iterator problem.

Statement

Design an iterator class ZigzagIterator that takes two integer arrays v1 and v2 and iterates through their elements by alternating between the arrays.

The iterator must support the following methods:

  • next() returns the next integer in the alternating traversal.

  • hasNext() returns true if there is at least one remaining element to return, otherwise returns false.

When one of the arrays runs out of elements, the iterator should continue returning the remaining elements from the other array in order.

Note: For the follow up, if more than two arrays are provided, interpret the order as cyclic across the arrays, skipping any array that has no remaining elements.

Constraints:

  • 0≤0 \leq v1.length, v2.length ≤1000\leq 1000

  • 1≤1 \leq v1.length ++ v2.length ≤2000\leq 2000

  • −231≤-2^{31} \leq v1[i], v2[i] ≤231−1\leq 2^{31} - 1