Skip to content

Commit f13e03c

Browse files
committed
public class ZigzagIterator281 {
1 parent 4ae3bdb commit f13e03c

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 293
47+
# Total: 294
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 77 | 161 | 52 | 3 |
51+
| 77 | 162 | 52 | 3 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -231,6 +231,7 @@
231231
| [278. First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FirstBadVersion278.java) | Easy |
232232
| [279. Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/PerfectSquares279.java) | Medium |
233233
| [280. Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/WiggleSort280.java) | Medium |
234+
| [281. Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ZigzagIterator281.java) | Medium |
234235
| [282. Expression Add Operators](https://leetcode.com/problems/expression-add-operators/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ExpressionAddOperators282.java) | Hard |
235236
| [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MoveZeroes283.java) | Easy |
236237
| [284. Peeking Iterator](https://leetcode.com/problems/peeking-iterator/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/PeekingIterator284.java) | Medium |

src/ZigzagIterator281.java

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)