Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a78bd8

Browse files
committed
281 (1) first try
1 parentd38e8d2 commit6a78bd8

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+
importjava.util.List;
34+
35+
/** see test {@link _281_ZigzagIterator.PracticeTest } */
36+
publicclassPractice {
37+
38+
39+
publicPractice(List<Integer>v1,List<Integer>v2) {
40+
}
41+
42+
publicintnext() {
43+
return0;
44+
}
45+
46+
publicbooleanhasNext() {
47+
returnfalse;
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+
importjava.util.Iterator;
37+
importjava.util.List;
38+
39+
/** see test {@link _281_ZigzagIterator.SolutionTest } */
40+
publicclassSolution {
41+
42+
Iterator<Integer>i1;
43+
44+
Iterator<Integer>i2;
45+
46+
Iterator<Integer>cur;
47+
48+
publicSolution(List<Integer>v1,List<Integer>v2) {
49+
i1 =v1.iterator();
50+
i2 =v2.iterator();
51+
cur =i1;
52+
}
53+
54+
publicintnext() {
55+
intnext = -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+
returnnext;
64+
}
65+
66+
publicbooleanhasNext() {
67+
returni1.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+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.Arrays;
7+
importjava.util.List;
8+
9+
importorg.junit.Rule;
10+
importorg.junit.Test;
11+
importorg.junit.rules.Timeout;
12+
13+
publicclassPracticeTest {
14+
15+
/** Test method for {@link _281_ZigzagIterator.Practice } */
16+
Practicesolution;
17+
18+
@Rule
19+
publicTimeoutglobalTimeout =newTimeout(200);
20+
21+
@Test
22+
publicvoidTest1() {
23+
List<Integer>v1 =Arrays.asList(1,2);
24+
List<Integer>v2 =Arrays.asList(3,4,5,6);
25+
Practicei =newPractice(v1,v2);
26+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest2() {
36+
List<Integer>v1 =Arrays.asList(1,2);
37+
List<Integer>v2 =Arrays.asList(3);
38+
Practicei =newPractice(v1,v2);
39+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest3() {
49+
List<Integer>v1 =Arrays.asList();
50+
List<Integer>v2 =Arrays.asList(3,5);
51+
Practicei =newPractice(v1,v2);
52+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest4() {
62+
List<Integer>v1 =Arrays.asList(4);
63+
List<Integer>v2 =Arrays.asList();
64+
Practicei =newPractice(v1,v2);
65+
List<Integer>actual =newArrayList<>();
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+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.Arrays;
7+
importjava.util.List;
8+
9+
importorg.junit.Rule;
10+
importorg.junit.Test;
11+
importorg.junit.rules.Timeout;
12+
13+
publicclassSolutionTest {
14+
15+
/** Test method for {@link _281_ZigzagIterator.Solution } */
16+
Solutionsolution;
17+
18+
@Rule
19+
publicTimeoutglobalTimeout =newTimeout(200);
20+
21+
@Test
22+
publicvoidTest1() {
23+
List<Integer>v1 =Arrays.asList(1,2);
24+
List<Integer>v2 =Arrays.asList(3,4,5,6);
25+
Solutioni =newSolution(v1,v2);
26+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest2() {
36+
List<Integer>v1 =Arrays.asList(1,2);
37+
List<Integer>v2 =Arrays.asList(3);
38+
Solutioni =newSolution(v1,v2);
39+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest3() {
49+
List<Integer>v1 =Arrays.asList();
50+
List<Integer>v2 =Arrays.asList(3,5);
51+
Solutioni =newSolution(v1,v2);
52+
List<Integer>actual =newArrayList<>();
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+
publicvoidTest4() {
62+
List<Integer>v1 =Arrays.asList(4);
63+
List<Integer>v2 =Arrays.asList();
64+
Solutioni =newSolution(v1,v2);
65+
List<Integer>actual =newArrayList<>();
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)

[8]ページ先頭

©2009-2025 Movatter.jp