-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathZigzagIterator281.java
113 lines (99 loc) · 3.08 KB
/
ZigzagIterator281.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* 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].
*/
public class ZigzagIterator281 {
class ZigzagIterator {
private int total;
private int k;
private int pos = 0;
private int index = 0;
private List<Integer>[] cache;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
this.cache = new List[2];
this.cache[0] = v1;
this.cache[1] = v2;
this.total = v1.size() + v2.size();
this.k = 2;
}
public int next() {
int x = this.index % k;
int y = this.index / k;
while (y >= this.cache[x].size()) {
this.index++;
x = this.index % k;
y = this.index / k;
}
int res = this.cache[x].get(y);
this.index++;
this.pos++;
return res;
}
public boolean hasNext() {
return this.pos < this.total;
}
}
/**
* https://leetcode.com/problems/zigzag-iterator/discuss/71779/Simple-Java-solution-for-K-vector
*/
class ZigzagIterator2 {
LinkedList<Iterator> list;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
list = new LinkedList<Iterator>();
if(!v1.isEmpty()) list.add(v1.iterator());
if(!v2.isEmpty()) list.add(v2.iterator());
}
public int next() {
Iterator poll = list.remove();
int result = (Integer)poll.next();
if(poll.hasNext()) list.add(poll);
return result;
}
public boolean hasNext() {
return !list.isEmpty();
}
}
/**
* https://leetcode.com/problems/zigzag-iterator/discuss/71781/Short-Java-O(1)-space
*/
class ZigzagIterator3 {
private Iterator<Integer> i, j, tmp;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
i = v2.iterator();
j = v1.iterator();
}
public int next() {
if (j.hasNext()) { tmp = j; j = i; i = tmp; }
return i.next();
}
public boolean hasNext() {
return i.hasNext() || j.hasNext();
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/
}