281. Zigzag Iterator

Given two 1d vectors, implement an iterator to return their elements alternately.

 

Example:

Input:
v1 = [1,2]
v2 = [3,4,5,6] 
Output: [1,3,2,4,5,6]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,3,2,4,5,6].

 

Follow up:

What if you are given k 1d vectors? How well can your code be extended to such cases?

Clarification for the follow up question:
The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example:

Input:
[1,2,3]
[4,5,6,7]
[8,9]

Output: [1,4,8,2,5,9,3,6,7].

Rust Solution

use std::collections::VecDeque;
use std::iter::FromIterator;

#[derive(Debug)]
struct ZigzagIterator {
    queues: VecDeque<VecDeque<i32>>,
}

impl<'a> ZigzagIterator {
    fn new(v1: Vec<i32>, v2: Vec<i32>) -> Self {
        let mut queues = VecDeque::new();
        if !v1.is_empty() {
            queues.push_back(VecDeque::from_iter(v1));
        }
        if !v2.is_empty() {
            queues.push_back(VecDeque::from_iter(v2));
        }
        ZigzagIterator { queues }
    }

    fn next(&mut self) -> i32 {
        let mut first = self.queues.pop_front().unwrap();
        let res = first.pop_front().unwrap();
        if !first.is_empty() {
            self.queues.push_back(first);
        }
        res
    }

    fn has_next(&self) -> bool {
        !self.queues.is_empty()
    }
}

#[test]
fn test() {
    let v1 = vec![1, 2];
    let v2 = vec![3, 4, 5, 6];
    let res = vec![1, 3, 2, 4, 5, 6];
    let mut obj = ZigzagIterator::new(v1, v2);
    let mut ans = vec![];
    while obj.has_next() {
        ans.push(obj.next());
    }
    assert_eq!(ans, res);
}

Having problems with this solution? Click here to submit an issue on github.