|
| 1 | +package by.andd3dfx.iterators; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Deque; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.List; |
| 7 | +import java.util.NoSuchElementException; |
| 8 | + |
| 9 | +/** |
| 10 | + * <pre> |
| 11 | + * You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it. |
| 12 | + * |
| 13 | + * Implement the NestedIterator class: |
| 14 | + * |
| 15 | + * NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList. |
| 16 | + * int next() Returns the next integer in the nested list. |
| 17 | + * boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise. |
| 18 | + * |
| 19 | + * Your code will be tested with the following pseudocode: |
| 20 | + * |
| 21 | + * initialize iterator with nestedList |
| 22 | + * res = [] |
| 23 | + * while iterator.hasNext() |
| 24 | + * append iterator.next() to the end of res |
| 25 | + * return res |
| 26 | + * |
| 27 | + * If res matches the expected flattened list, then your code will be judged as correct. |
| 28 | + * |
| 29 | + * Example 1: |
| 30 | + * Input: nestedList = [[1,1],2,[1,1]] |
| 31 | + * Output: [1,1,2,1,1] |
| 32 | + * Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. |
| 33 | + * |
| 34 | + * Example 2: |
| 35 | + * Input: nestedList = [1,[4,[6]]] |
| 36 | + * Output: [1,4,6] |
| 37 | + * Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. |
| 38 | + * </pre> |
| 39 | + */ |
| 40 | +public class NestedIterator implements Iterator<Integer> { |
| 41 | + |
| 42 | + private final Deque<Iterator<INestedInteger>> stack = new ArrayDeque<>(); |
| 43 | + |
| 44 | + public NestedIterator(List<INestedInteger> nestedList) { |
| 45 | + stack.push(nestedList.iterator()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Integer next() { |
| 50 | + if (stack.isEmpty()) { |
| 51 | + throw new NoSuchElementException(); |
| 52 | + } |
| 53 | + |
| 54 | + var currentIterator = stack.peek(); |
| 55 | + if (!currentIterator.hasNext()) { |
| 56 | + stack.pop(); |
| 57 | + return next(); |
| 58 | + } |
| 59 | + |
| 60 | + INestedInteger object = currentIterator.next(); |
| 61 | + if (object.isInteger()) { |
| 62 | + return object.getInteger(); |
| 63 | + } |
| 64 | + stack.push(object.getList().iterator()); |
| 65 | + return next(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean hasNext() { |
| 70 | + if (stack.isEmpty()) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + var currentIterator = stack.peek(); |
| 75 | + if (currentIterator.hasNext()) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + stack.pop(); |
| 79 | + return hasNext(); |
| 80 | + } |
| 81 | + |
| 82 | + public interface INestedInteger { |
| 83 | + |
| 84 | + // @return true if this NestedInteger holds a single integer, rather than a nested list. |
| 85 | + boolean isInteger(); |
| 86 | + |
| 87 | + // @return the single integer that this NestedInteger holds, if it holds a single integer |
| 88 | + // Return null if this NestedInteger holds a nested list |
| 89 | + Integer getInteger(); |
| 90 | + |
| 91 | + // @return the nested list that this NestedInteger holds, if it holds a nested list |
| 92 | + // Return empty list if this NestedInteger holds a single integer |
| 93 | + List<INestedInteger> getList(); |
| 94 | + } |
| 95 | +} |
0 commit comments