Unable to understand Follow-up


  • 0
    R

    I'm having trouble understanding what the followup question for zigzag iterator means when they say to extend it for k-1Dvectors, will the ZigzagIterator take k lists now instead of 2?

    The following is my OJ passed code for the 2D case, suggest how to extend this to k. I used the ctr[] to maybe just loop through it if I'm getting k lists instead of 2.

    public class ZigzagIterator {
        List<Integer> ziggy=new ArrayList<Integer>();
        public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
            if(v1.size()==0) {
                ziggy.addAll(new ArrayList(v2));
                return;
            }
            if(v2.size()==0) {
                ziggy.addAll(new ArrayList(v1));
                return;
            }
            int[] ctr= new int[2];
            while(ctr[0]<v1.size() && ctr[1]<v2.size()) {
                ziggy.add(v1.get(ctr[0]++));
                ziggy.add(v2.get(ctr[1]++));
            }
            while(ctr[0]<v1.size()) ziggy.add(v1.get(ctr[0]++));
            while(ctr[1]<v2.size()) ziggy.add(v2.get(ctr[1]++));
        }
    
        public int next() {
            return ziggy.remove(0);
        }
    
        public boolean hasNext() {
            return !(ziggy.size()==0);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.