Skip to content

Commit 6a78bd8

Browse files
committed
281 (1) first try
1 parent d38e8d2 commit 6a78bd8

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

src/_281_ZigzagIterator/Practice.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
***************************************************************************
3+
* Description:
4+
*
5+
* Given two 1d vectors, implement an iterator to return their elements alternately.
6+
*
7+
* For example, given two 1d vectors:
8+
* v1 = [1, 2]
9+
* v2 = [3, 4, 5, 6]
10+
* By calling next repeatedly until hasNext returns false, the order of elements
11+
* returned by next should be: [1, 3, 2, 4, 5, 6].
12+
*
13+
* Follow up: What if you are given k 1d vectors?
14+
* How well can your code be extended to such cases?
15+
*
16+
* Clarification for the follow up question - Update (2015-09-18):
17+
* The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases.
18+
* If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic".
19+
* For example, given the following input:
20+
*
21+
* [1,2,3]
22+
* [4,5,6,7]
23+
* [8,9]
24+
*
25+
* It should return [1,4,8,2,5,9,3,6,7].
26+
*
27+
***************************************************************************
28+
* @tag : Design
29+
* {@link https://leetcode.com/problems/zigzag-iterator/ }
30+
*/
31+
package _281_ZigzagIterator;
32+
33+
import java.util.List;
34+
35+
/** see test {@link _281_ZigzagIterator.PracticeTest } */
36+
public class Practice {
37+
38+
39+
public Practice(List<Integer> v1, List<Integer> v2) {
40+
}
41+
42+
public int next() {
43+
return 0;
44+
}
45+
46+
public boolean hasNext() {
47+
return false;
48+
}
49+
50+
}

src/_281_ZigzagIterator/Solution.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : Design
4+
* @by : Steven Cooks
5+
* @date: Oct 2, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given two 1d vectors, implement an iterator to return their elements alternately.
10+
*
11+
* For example, given two 1d vectors:
12+
* v1 = [1, 2]
13+
* v2 = [3, 4, 5, 6]
14+
* By calling next repeatedly until hasNext returns false, the order of elements
15+
* returned by next should be: [1, 3, 2, 4, 5, 6].
16+
*
17+
* Follow up: What if you are given k 1d vectors?
18+
* How well can your code be extended to such cases?
19+
*
20+
* Clarification for the follow up question - Update (2015-09-18):
21+
* The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases.
22+
* If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic".
23+
* For example, given the following input:
24+
*
25+
* [1,2,3]
26+
* [4,5,6,7]
27+
* [8,9]
28+
*
29+
* It should return [1,4,8,2,5,9,3,6,7].
30+
*
31+
***************************************************************************
32+
* {@link https://leetcode.com/problems/zigzag-iterator/ }
33+
*/
34+
package _281_ZigzagIterator;
35+
36+
import java.util.Iterator;
37+
import java.util.List;
38+
39+
/** see test {@link _281_ZigzagIterator.SolutionTest } */
40+
public class Solution {
41+
42+
Iterator<Integer> i1;
43+
44+
Iterator<Integer> i2;
45+
46+
Iterator<Integer> cur;
47+
48+
public Solution(List<Integer> v1, List<Integer> v2) {
49+
i1 = v1.iterator();
50+
i2 = v2.iterator();
51+
cur = i1;
52+
}
53+
54+
public int next() {
55+
int next = -1;
56+
if (cur == i1) {
57+
next = i1.hasNext() ? i1.next() : i2.next();
58+
cur = i2;
59+
} else {
60+
next = i2.hasNext() ? i2.next() : i1.next();
61+
cur = i1;
62+
}
63+
return next;
64+
}
65+
66+
public boolean hasNext() {
67+
return i1.hasNext() || i2.hasNext();
68+
}
69+
70+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package _281_ZigzagIterator;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.Timeout;
12+
13+
public class PracticeTest {
14+
15+
/** Test method for {@link _281_ZigzagIterator.Practice } */
16+
Practice solution;
17+
18+
@Rule
19+
public Timeout globalTimeout = new Timeout(200);
20+
21+
@Test
22+
public void Test1() {
23+
List<Integer> v1 = Arrays.asList(1, 2);
24+
List<Integer> v2 = Arrays.asList(3, 4, 5, 6);
25+
Practice i = new Practice(v1, v2);
26+
List<Integer> actual = new ArrayList<>();
27+
while (i.hasNext()) {
28+
actual.add(i.next());
29+
}
30+
List<Integer> expected = Arrays.asList(1, 3, 2, 4, 5, 6);
31+
assertEquals(expected, actual);
32+
}
33+
34+
@Test
35+
public void Test2() {
36+
List<Integer> v1 = Arrays.asList(1, 2);
37+
List<Integer> v2 = Arrays.asList(3);
38+
Practice i = new Practice(v1, v2);
39+
List<Integer> actual = new ArrayList<>();
40+
while (i.hasNext()) {
41+
actual.add(i.next());
42+
}
43+
List<Integer> expected = Arrays.asList(1, 3, 2);
44+
assertEquals(expected, actual);
45+
}
46+
47+
@Test
48+
public void Test3() {
49+
List<Integer> v1 = Arrays.asList();
50+
List<Integer> v2 = Arrays.asList(3, 5);
51+
Practice i = new Practice(v1, v2);
52+
List<Integer> actual = new ArrayList<>();
53+
while (i.hasNext()) {
54+
actual.add(i.next());
55+
}
56+
List<Integer> expected = Arrays.asList(3, 5);
57+
assertEquals(expected, actual);
58+
}
59+
60+
@Test
61+
public void Test4() {
62+
List<Integer> v1 = Arrays.asList(4);
63+
List<Integer> v2 = Arrays.asList();
64+
Practice i = new Practice(v1, v2);
65+
List<Integer> actual = new ArrayList<>();
66+
while (i.hasNext()) {
67+
actual.add(i.next());
68+
}
69+
List<Integer> expected = Arrays.asList(4);
70+
assertEquals(expected, actual);
71+
}
72+
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package _281_ZigzagIterator;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.Timeout;
12+
13+
public class SolutionTest {
14+
15+
/** Test method for {@link _281_ZigzagIterator.Solution } */
16+
Solution solution;
17+
18+
@Rule
19+
public Timeout globalTimeout = new Timeout(200);
20+
21+
@Test
22+
public void Test1() {
23+
List<Integer> v1 = Arrays.asList(1, 2);
24+
List<Integer> v2 = Arrays.asList(3, 4, 5, 6);
25+
Solution i = new Solution(v1, v2);
26+
List<Integer> actual = new ArrayList<>();
27+
while (i.hasNext()) {
28+
actual.add(i.next());
29+
}
30+
List<Integer> expected = Arrays.asList(1, 3, 2, 4, 5, 6);
31+
assertEquals(expected, actual);
32+
}
33+
34+
@Test
35+
public void Test2() {
36+
List<Integer> v1 = Arrays.asList(1, 2);
37+
List<Integer> v2 = Arrays.asList(3);
38+
Solution i = new Solution(v1, v2);
39+
List<Integer> actual = new ArrayList<>();
40+
while (i.hasNext()) {
41+
actual.add(i.next());
42+
}
43+
List<Integer> expected = Arrays.asList(1, 3, 2);
44+
assertEquals(expected, actual);
45+
}
46+
47+
@Test
48+
public void Test3() {
49+
List<Integer> v1 = Arrays.asList();
50+
List<Integer> v2 = Arrays.asList(3, 5);
51+
Solution i = new Solution(v1, v2);
52+
List<Integer> actual = new ArrayList<>();
53+
while (i.hasNext()) {
54+
actual.add(i.next());
55+
}
56+
List<Integer> expected = Arrays.asList(3, 5);
57+
assertEquals(expected, actual);
58+
}
59+
60+
@Test
61+
public void Test4() {
62+
List<Integer> v1 = Arrays.asList(4);
63+
List<Integer> v2 = Arrays.asList();
64+
Solution i = new Solution(v1, v2);
65+
List<Integer> actual = new ArrayList<>();
66+
while (i.hasNext()) {
67+
actual.add(i.next());
68+
}
69+
List<Integer> expected = Arrays.asList(4);
70+
assertEquals(expected, actual);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy