| 
1 | 1 | package com.fishercoder.solutions;  | 
2 | 2 | 
 
  | 
3 | 3 | import java.util.Iterator;  | 
 | 4 | +import java.util.LinkedList;  | 
4 | 5 | import java.util.List;  | 
 | 6 | +import java.util.Queue;  | 
5 | 7 | 
 
  | 
6 | 8 | /**  | 
 | 9 | + * 281. Zigzag Iterator  | 
 | 10 | + *  | 
7 | 11 |  * Given two 1d vectors, implement an iterator to return their elements alternately.  | 
8 | 12 | 
  | 
9 | 13 |  For example, given two 1d vectors:  | 
 | 
15 | 19 |  Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?  | 
16 | 20 | 
  | 
17 | 21 |  Clarification for the follow up question - Update (2015-09-18):  | 
18 |  | - 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, given the following input:  | 
 | 22 | + The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases.  | 
 | 23 | + If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example, given the following input:  | 
19 | 24 | 
  | 
20 | 25 |  [1,2,3]  | 
21 | 26 |  [4,5,6,7]  | 
 | 
24 | 29 |  */  | 
25 | 30 | public class _281 {  | 
26 | 31 | 
 
  | 
27 |  | -    private Iterator<Integer> i;  | 
28 |  | -    private Iterator<Integer> j;  | 
29 |  | -    private Iterator<Integer> tmp;  | 
 | 32 | +    public static class Solution1 {  | 
 | 33 | +        public static class ZigzagIterator {  | 
 | 34 | +            private Iterator<Integer> i;  | 
 | 35 | +            private Iterator<Integer> j;  | 
 | 36 | +            private Iterator<Integer> tmp;  | 
30 | 37 | 
 
  | 
31 |  | -    public _281(List<Integer> v1, List<Integer> v2) {  | 
32 |  | -        i = v2.iterator();  | 
33 |  | -        j = v1.iterator();  | 
34 |  | -    }  | 
 | 38 | +            public ZigzagIterator(List<Integer> v1, List<Integer> v2) {  | 
 | 39 | +                i = v2.iterator();  | 
 | 40 | +                j = v1.iterator();  | 
 | 41 | +            }  | 
 | 42 | + | 
 | 43 | +            public int next() {  | 
 | 44 | +                if (j.hasNext()) {  | 
 | 45 | +                    tmp = j;  | 
 | 46 | +                    j = i;  | 
 | 47 | +                    i = tmp;  | 
 | 48 | +                }  | 
 | 49 | +                return i.next();  | 
 | 50 | +            }  | 
35 | 51 | 
 
  | 
36 |  | -    public int next() {  | 
37 |  | -        if (j.hasNext()) {  | 
38 |  | -            tmp = j;  | 
39 |  | -            j = i;  | 
40 |  | -            i = tmp;  | 
 | 52 | +            public boolean hasNext() {  | 
 | 53 | +                return i.hasNext() || j.hasNext();  | 
 | 54 | +            }  | 
41 | 55 |         }  | 
42 |  | -        return i.next();  | 
43 | 56 |     }  | 
44 | 57 | 
 
  | 
45 |  | -    public boolean hasNext() {  | 
46 |  | -        return i.hasNext() || j.hasNext();  | 
47 |  | -    }  | 
 | 58 | +    public static class Solution2 {  | 
 | 59 | +        public static class ZigzagIterator {  | 
 | 60 | + | 
 | 61 | +            Queue<Iterator<Integer>> queue;  | 
48 | 62 | 
 
  | 
 | 63 | +            public ZigzagIterator(List<Integer> v1, List<Integer> v2) {  | 
 | 64 | +                queue = new LinkedList<>();  | 
 | 65 | +                if (v1 != null && !v1.isEmpty()) {  | 
 | 66 | +                    Iterator<Integer> iterator1 = v1.iterator();  | 
 | 67 | +                    queue.offer(iterator1);  | 
 | 68 | +                }  | 
 | 69 | +                if (v2 != null && !v2.isEmpty()) {  | 
 | 70 | +                    Iterator<Integer> iterator2 = v2.iterator();  | 
 | 71 | +                    queue.offer(iterator2);  | 
 | 72 | +                }  | 
 | 73 | +            }  | 
 | 74 | + | 
 | 75 | +            public boolean hasNext() {  | 
 | 76 | +                return !queue.isEmpty();  | 
 | 77 | +            }  | 
 | 78 | + | 
 | 79 | +            public int next() {  | 
 | 80 | +                Iterator<Integer> iterator = queue.poll();  | 
 | 81 | +                int next = iterator.next();  | 
 | 82 | +                if (iterator.hasNext()) {  | 
 | 83 | +                    queue.offer(iterator);  | 
 | 84 | +                }  | 
 | 85 | +                return next;  | 
 | 86 | +            }  | 
 | 87 | +        }  | 
 | 88 | +    }  | 
49 | 89 | }  | 
0 commit comments