Back to course home
0% completed
Vote For New Content
Zigzag Iterator (medium)
Problem Statement
Given two 1d
vectors, implement an iterator to return their elements alternately.
Implement the Solution
class:
Solution(List<int> v1, List<int> v2)
is a constructor.- int
next()
returns the current element of the iterator and moves the iterator to the next element. - boolean
hasNext()
returns true if the iterator still has elements, and false otherwise.
Examples
Example 1
- Input: V1 = [1,2], v2 = [3,4,5,6]
- Expected Output: [1,3,2,4,5,6]
- Explanation: The elements will be returned in [1,3,2,4,5,6] order when we make v1.size() + v2.size() number of calls to the next() method.
Example 2
- Input: V1 = [1, 2, 3, 4], v2 = [5,6]
- Expected Output: [1,5,2,6,3,4]
- Explanation: The elements will be returned in [1,5,2,6,3,4] order when we make v1.size() + v2.size() number of calls to the next() method.
Example 3
- Input: V1 = [1, 2], v2 = []
- Expected Output: [1,2]
- Explanation: The elements will be returned in [1, 2] order when we make v1.size() + v2.size() number of calls to the next() method.
Constraints:
0 <= v1.length, v2.length <= 1000
1 <= v1.length + v2.length <= 2000
- -2<sup>31</sup> <= v1[i], v2[i] <= 2<sup>31</sup> - 1
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself