|
| 1 | +/** |
| 2 | + * Given two 1d vectors, implement an iterator to return their elements alternately. |
| 3 | + * |
| 4 | + * Example: |
| 5 | + * Input: |
| 6 | + * v1 = [1,2] |
| 7 | + * v2 = [3,4,5,6] |
| 8 | + * Output: [1,3,2,4,5,6] |
| 9 | + * Explanation: By calling next repeatedly until hasNext returns false, |
| 10 | + * the order of elements returned by next should be: [1,3,2,4,5,6]. |
| 11 | + * |
| 12 | + * Follow up: What if you are given k 1d vectors? How well can your code be |
| 13 | + * extended to such cases? |
| 14 | + * |
| 15 | + * Clarification for the follow up question: |
| 16 | + * The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. |
| 17 | + * If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". |
| 18 | + * |
| 19 | + * For example: |
| 20 | + * Input: |
| 21 | + * [1,2,3] |
| 22 | + * [4,5,6,7] |
| 23 | + * [8,9] |
| 24 | + * Output: [1,4,8,2,5,9,3,6,7]. |
| 25 | + */ |
| 26 | + |
| 27 | +public class ZigzagIterator281 { |
| 28 | + class ZigzagIterator { |
| 29 | + private int total; |
| 30 | + private int k; |
| 31 | + private int pos = 0; |
| 32 | + private int index = 0; |
| 33 | + private List<Integer>[] cache; |
| 34 | + |
| 35 | + public ZigzagIterator(List<Integer> v1, List<Integer> v2) { |
| 36 | + this.cache = new List[2]; |
| 37 | + this.cache[0] = v1; |
| 38 | + this.cache[1] = v2; |
| 39 | + this.total = v1.size() + v2.size(); |
| 40 | + this.k = 2; |
| 41 | + } |
| 42 | + |
| 43 | + public int next() { |
| 44 | + int x = this.index % k; |
| 45 | + int y = this.index / k; |
| 46 | + while (y >= this.cache[x].size()) { |
| 47 | + this.index++; |
| 48 | + x = this.index % k; |
| 49 | + y = this.index / k; |
| 50 | + } |
| 51 | + int res = this.cache[x].get(y); |
| 52 | + this.index++; |
| 53 | + this.pos++; |
| 54 | + return res; |
| 55 | + } |
| 56 | + |
| 57 | + public boolean hasNext() { |
| 58 | + return this.pos < this.total; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * https://leetcode.com/problems/zigzag-iterator/discuss/71779/Simple-Java-solution-for-K-vector |
| 64 | + */ |
| 65 | + class ZigzagIterator2 { |
| 66 | + LinkedList<Iterator> list; |
| 67 | + public ZigzagIterator(List<Integer> v1, List<Integer> v2) { |
| 68 | + list = new LinkedList<Iterator>(); |
| 69 | + if(!v1.isEmpty()) list.add(v1.iterator()); |
| 70 | + if(!v2.isEmpty()) list.add(v2.iterator()); |
| 71 | + } |
| 72 | + |
| 73 | + public int next() { |
| 74 | + Iterator poll = list.remove(); |
| 75 | + int result = (Integer)poll.next(); |
| 76 | + if(poll.hasNext()) list.add(poll); |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + public boolean hasNext() { |
| 81 | + return !list.isEmpty(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * https://leetcode.com/problems/zigzag-iterator/discuss/71781/Short-Java-O(1)-space |
| 87 | + */ |
| 88 | + class ZigzagIterator3 { |
| 89 | + private Iterator<Integer> i, j, tmp; |
| 90 | + |
| 91 | + public ZigzagIterator(List<Integer> v1, List<Integer> v2) { |
| 92 | + i = v2.iterator(); |
| 93 | + j = v1.iterator(); |
| 94 | + } |
| 95 | + |
| 96 | + public int next() { |
| 97 | + if (j.hasNext()) { tmp = j; j = i; i = tmp; } |
| 98 | + return i.next(); |
| 99 | + } |
| 100 | + |
| 101 | + public boolean hasNext() { |
| 102 | + return i.hasNext() || j.hasNext(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +/** |
| 107 | + * Your ZigzagIterator object will be instantiated and called as such: |
| 108 | + * ZigzagIterator i = new ZigzagIterator(v1, v2); |
| 109 | + * while (i.hasNext()) v[f()] = i.next(); |
| 110 | + */ |
| 111 | + |
| 112 | +} |
| 113 | + |
0 commit comments