|
| 1 | +# 341. Flatten Nested List Iterator |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Stack, Tree, Depth-First Search, Design, Queue, Iterator. |
| 5 | +- Similar Questions: Flatten 2D Vector, Zigzag Iterator, Mini Parser, Array Nesting. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +Implement the `NestedIterator` class: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`. |
| 16 | + |
| 17 | +- `int next()` Returns the next integer in the nested list. |
| 18 | + |
| 19 | +- `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise. |
| 20 | + |
| 21 | + |
| 22 | +Your code will be tested with the following pseudocode: |
| 23 | + |
| 24 | +``` |
| 25 | +initialize iterator with nestedList |
| 26 | +res = [] |
| 27 | +while iterator.hasNext() |
| 28 | + append iterator.next() to the end of res |
| 29 | +return res |
| 30 | +``` |
| 31 | + |
| 32 | +If `res` matches the expected flattened list, then your code will be judged as correct. |
| 33 | + |
| 34 | + |
| 35 | +Example 1: |
| 36 | + |
| 37 | +``` |
| 38 | +Input: nestedList = [[1,1],2,[1,1]] |
| 39 | +Output: [1,1,2,1,1] |
| 40 | +Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. |
| 41 | +``` |
| 42 | + |
| 43 | +Example 2: |
| 44 | + |
| 45 | +``` |
| 46 | +Input: nestedList = [1,[4,[6]]] |
| 47 | +Output: [1,4,6] |
| 48 | +Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +**Constraints:** |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +- `1 <= nestedList.length <= 500` |
| 57 | + |
| 58 | +- The values of the integers in the nested list is in the range `[-106, 106]`. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Solution |
| 63 | + |
| 64 | +```javascript |
| 65 | +/** |
| 66 | + * // This is the interface that allows for creating nested lists. |
| 67 | + * // You should not implement it, or speculate about its implementation |
| 68 | + * function NestedInteger() { |
| 69 | + * |
| 70 | + * Return true if this NestedInteger holds a single integer, rather than a nested list. |
| 71 | + * @return {boolean} |
| 72 | + * this.isInteger = function() { |
| 73 | + * ... |
| 74 | + * }; |
| 75 | + * |
| 76 | + * Return the single integer that this NestedInteger holds, if it holds a single integer |
| 77 | + * Return null if this NestedInteger holds a nested list |
| 78 | + * @return {integer} |
| 79 | + * this.getInteger = function() { |
| 80 | + * ... |
| 81 | + * }; |
| 82 | + * |
| 83 | + * Return the nested list that this NestedInteger holds, if it holds a nested list |
| 84 | + * Return null if this NestedInteger holds a single integer |
| 85 | + * @return {NestedInteger[]} |
| 86 | + * this.getList = function() { |
| 87 | + * ... |
| 88 | + * }; |
| 89 | + * }; |
| 90 | + */ |
| 91 | +/** |
| 92 | + * @constructor |
| 93 | + * @param {NestedInteger[]} nestedList |
| 94 | + */ |
| 95 | +var NestedIterator = function(nestedList) { |
| 96 | + this.index = 0; |
| 97 | + this.list = []; |
| 98 | + const flatten = (list) => { |
| 99 | + for (var item of list) { |
| 100 | + if (item.isInteger()) { |
| 101 | + this.list.push(item.getInteger()); |
| 102 | + } else { |
| 103 | + flatten(item.getList()); |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + flatten(nestedList); |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +/** |
| 112 | + * @this NestedIterator |
| 113 | + * @returns {boolean} |
| 114 | + */ |
| 115 | +NestedIterator.prototype.hasNext = function() { |
| 116 | + return this.index < this.list.length; |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * @this NestedIterator |
| 121 | + * @returns {integer} |
| 122 | + */ |
| 123 | +NestedIterator.prototype.next = function() { |
| 124 | + return this.list[this.index++]; |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * Your NestedIterator will be called like this: |
| 129 | + * var i = new NestedIterator(nestedList), a = []; |
| 130 | + * while (i.hasNext()) a.push(i.next()); |
| 131 | +*/ |
| 132 | +``` |
| 133 | + |
| 134 | +**Explain:** |
| 135 | + |
| 136 | +nope. |
| 137 | + |
| 138 | +**Complexity:** |
| 139 | + |
| 140 | +* Time complexity : O(n). |
| 141 | +* Space complexity : O(n). |
0 commit comments