Skip to content

Commit f25b7ad

Browse files
committed
flatten nested iterator
1 parent 6730cbd commit f25b7ad

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package joshua.leetcode.design;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @author Joshua.Jiang on 2016/5/22.
7+
*/
8+
public abstract class NestedIterator implements Iterator<Integer> {
9+
10+
/**
11+
* constructor for a NestedIterator
12+
*
13+
* @param nestedList the target to iterator on
14+
*/
15+
public NestedIterator(List<NestedInteger> nestedList) {}
16+
17+
public static class Solution1 extends NestedIterator {
18+
19+
/**
20+
* in java, deque take precedes over Stack class when you want a stack implementation.
21+
*/
22+
Deque<NestedInteger> stack = new LinkedList<NestedInteger>();
23+
24+
public Solution1(List<NestedInteger> nestedList) {
25+
super(nestedList);
26+
for(int i = nestedList.size() -1 ; i > -1; i--) {
27+
stack.addLast(nestedList.get(i));
28+
}
29+
}
30+
31+
@Override
32+
public boolean hasNext() {
33+
while(!stack.isEmpty()) {
34+
NestedInteger top = stack.getLast();
35+
if (top.isInteger()) {
36+
return true;
37+
} else {
38+
stack.removeLast();
39+
for (int i = top.getList().size() - 1; i > -1; i--) {
40+
stack.addLast(top.getList().get(i));
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
47+
@Override
48+
public Integer next() {
49+
return stack.removeLast().getInteger();
50+
}
51+
52+
@Override
53+
public void remove() {}
54+
}
55+
}
56+
57+
interface NestedInteger {
58+
59+
/**
60+
*
61+
* @return true if this NestedInteger holds a single integer, rather than a nested list.
62+
*/
63+
public boolean isInteger();
64+
65+
/**
66+
*
67+
* @return the single integer that this NestedInteger holds, if it holds a single integer
68+
* Return null if this NestedInteger holds a nested list
69+
*/
70+
public Integer getInteger();
71+
72+
/**
73+
*
74+
* @return the nested list that this NestedInteger holds, if it holds a nested list,
75+
* Return null if this NestedInteger holds a single integer
76+
*/
77+
public List<NestedInteger> getList();
78+
}
79+
80+
class NestedIntegerImpl implements NestedInteger {
81+
82+
private Integer integer;
83+
84+
private List<NestedInteger> list;
85+
86+
public NestedIntegerImpl(Integer integer, NestedInteger[] list) {
87+
this.integer = integer;
88+
if (list != null)
89+
this.list = Arrays.asList(list);
90+
}
91+
92+
@Override
93+
public boolean isInteger() {
94+
return integer != null;
95+
}
96+
97+
@Override
98+
public Integer getInteger() {
99+
return integer;
100+
}
101+
102+
@Override
103+
public List<NestedInteger> getList() {
104+
return list;
105+
}
106+
}
107+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
package joshua.leetcode.design;
3+
4+
import com.google.common.collect.Lists;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.*;
12+
13+
public class NestedIteratorTest {
14+
15+
private NestedIterator solution;
16+
17+
List<NestedInteger> nestedIntegerList = Lists.newLinkedList();
18+
19+
@Before
20+
public void setUp() {
21+
}
22+
23+
@Test
24+
public void test1() {
25+
nestedIntegerList.add(new NestedIntegerImpl(null, new NestedInteger[]{new NestedIntegerImpl(1, null),
26+
new NestedIntegerImpl(1, null)}));
27+
nestedIntegerList.add(new NestedIntegerImpl(2, null));
28+
nestedIntegerList.add(new NestedIntegerImpl(null, new NestedInteger[]{new NestedIntegerImpl(1, null),
29+
new NestedIntegerImpl(1, null)}));
30+
solution = new NestedIterator.Solution1(nestedIntegerList);
31+
32+
List<Integer> flattenList = new LinkedList<Integer>();
33+
while(solution.hasNext()) {
34+
flattenList.add(solution.next());
35+
}
36+
assertEquals(Lists.newArrayList(1,1,2,1,1),flattenList);
37+
}
38+
39+
@Test
40+
public void test2() {
41+
List<NestedInteger> nestedIntegerList = Lists.newLinkedList();
42+
nestedIntegerList.add(new NestedIntegerImpl(1, null));
43+
nestedIntegerList.add(new NestedIntegerImpl(null, new NestedInteger[]{new NestedIntegerImpl(4, null),
44+
new NestedIntegerImpl(null, new NestedInteger[]{new NestedIntegerImpl(6,null)})}));
45+
46+
solution = new NestedIterator.Solution1(nestedIntegerList);
47+
48+
List<Integer> flattenList = new LinkedList<Integer>();
49+
while(solution.hasNext()) {
50+
flattenList.add(solution.next());
51+
}
52+
assertEquals(Lists.newArrayList(1,4,6),flattenList);
53+
}
54+
}

0 commit comments

Comments
 (0)