|
| 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 | + |
0 commit comments