|
| 1 | +# 341. Flatter Nested List Iterator |
| 2 | +# Topics: 'Stack', 'Tree', 'Depth-First Search', 'Design', 'Queue', 'Iterator' |
| 3 | +# Level: 'Medium' |
| 4 | + |
| 5 | +# 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. |
| 6 | + |
| 7 | +# Implement the NestedIterator class: |
| 8 | + |
| 9 | +# NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList. |
| 10 | +# int next() Returns the next integer in the nested list. |
| 11 | +# boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise. |
| 12 | + |
| 13 | +# Your code will be tested with the following pseudocode: |
| 14 | + |
| 15 | +# initialize iterator with nestedList |
| 16 | +# res = [] |
| 17 | +# while iterator.hasNext() |
| 18 | +# append iterator.next() to the end of res |
| 19 | +# return res |
| 20 | + |
| 21 | +# If res matches the expected flattened list, then your code will be judged as correct. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +# Example 1: |
| 26 | + |
| 27 | +# Input: nestedList = [[1,1],2,[1,1]] |
| 28 | +# Output: [1,1,2,1,1] |
| 29 | +# Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. |
| 30 | + |
| 31 | +# Example 2: |
| 32 | + |
| 33 | +# Input: nestedList = [1,[4,[6]]] |
| 34 | +# Output: [1,4,6] |
| 35 | +# Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +# Constraints: |
| 40 | + |
| 41 | +# 1 <= nestedList.length <= 500 |
| 42 | +# The values of the integers in the nested list is in the range [-106, 106]. |
| 43 | + |
| 44 | +# """ |
| 45 | +# This is the interface that allows for creating nested lists. |
| 46 | +# You should not implement it, or speculate about its implementation |
| 47 | +# """ |
| 48 | +# class NestedInteger: |
| 49 | +# def isInteger(self) -> bool: |
| 50 | +# """ |
| 51 | +# @return True if this NestedInteger holds a single integer, rather than a nested list. |
| 52 | +# """ |
| 53 | + |
| 54 | +# def getInteger(self) -> int: |
| 55 | +# """ |
| 56 | +# @return the single integer that this NestedInteger holds, if it holds a single integer |
| 57 | +# Return None if this NestedInteger holds a nested list |
| 58 | +# """ |
| 59 | + |
| 60 | +# def getList(self) -> [NestedInteger]: |
| 61 | +# """ |
| 62 | +# @return the nested list that this NestedInteger holds, if it holds a nested list |
| 63 | +# Return None if this NestedInteger holds a single integer |
| 64 | +# # """ |
| 65 | + |
| 66 | +# crazy good solution |
| 67 | +# extend stack if not a number |
| 68 | +# lazy initialization |
| 69 | +class NestedIterator: |
| 70 | + def __init__(self, nestedList: [NestedInteger]): |
| 71 | + self.stack = nestedList[::-1] |
| 72 | + |
| 73 | + def next(self) -> int: |
| 74 | + return self.stack.pop().getInteger() |
| 75 | + |
| 76 | + def hasNext(self) -> bool: |
| 77 | + while self.stack: |
| 78 | + top = self.stack[-1] |
| 79 | + if top.isInteger(): |
| 80 | + return True |
| 81 | + self.stack.extend(self.stack.pop().getList()[::-1]) |
| 82 | + return False |
| 83 | + |
| 84 | + |
| 85 | +# Lazy initialization each NestedInteger tree |
| 86 | +# class NestedIterator: |
| 87 | +# def __init__(self, nestedList: [NestedInteger]): |
| 88 | +# self.i = 0 |
| 89 | +# self.nestedList = nestedList |
| 90 | +# self.q = deque() |
| 91 | + |
| 92 | +# def next(self) -> int: |
| 93 | +# return self.q.popleft() |
| 94 | + |
| 95 | +# def hasNext(self) -> bool: |
| 96 | +# while not self.q: |
| 97 | +# if self.i >= len(self.nestedList): |
| 98 | +# break |
| 99 | +# self.dfs(self.nestedList[self.i]) |
| 100 | +# self.i+=1 |
| 101 | +# return self.q |
| 102 | + |
| 103 | +# def dfs(self, root: NestedInteger): |
| 104 | +# if not root: |
| 105 | +# return |
| 106 | +# else: |
| 107 | +# if root.isInteger(): |
| 108 | +# self.q.append(root.getInteger()) |
| 109 | +# else: |
| 110 | +# for ni in root.getList(): |
| 111 | +# self.dfs(ni) |
| 112 | + |
| 113 | +# Eager initialization |
| 114 | +# class NestedIterator: |
| 115 | +# def __init__(self, nestedList: [NestedInteger]): |
| 116 | +# self.arr = [] |
| 117 | +# self.i = 0 |
| 118 | +# def dfs(root: NestedInteger): |
| 119 | +# if not root: |
| 120 | +# return |
| 121 | +# if root.isInteger(): |
| 122 | +# self.arr.append(root.getInteger()) |
| 123 | +# else: |
| 124 | +# for ni in root.getList(): |
| 125 | +# dfs(ni) |
| 126 | +# for ni in nestedList: |
| 127 | +# dfs(ni) |
| 128 | + |
| 129 | + |
| 130 | +# def next(self) -> int: |
| 131 | +# v = self.arr[self.i] |
| 132 | +# self.i+=1 |
| 133 | +# return v |
| 134 | + |
| 135 | +# def hasNext(self) -> bool: |
| 136 | +# return self.i < len(self.arr) |
| 137 | + |
0 commit comments