Skip to content

Commit 5183eb0

Browse files
add 341
1 parent 034db12 commit 5183eb0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,46 @@ class CombinationIterator(characters: String, combinationLength: Int) {
245245
combine(index + 1, str, subset, store, limit)
246246
}
247247

248+
}
249+
250+
/**
251+
* 341. Flatten Nested List Iterator
252+
*/
253+
254+
class NestedInteger {
255+
fun getInteger(): Int? {
256+
return null
257+
}
258+
259+
fun getList(): List<NestedInteger>? {
260+
return null
261+
}
262+
}
263+
264+
class NestedIterator(nestedList: List<NestedInteger>) {
265+
266+
private val store = mutableListOf<Int>()
267+
268+
init {
269+
nestedList.forEach {
270+
dfs(it)
271+
}
272+
}
273+
274+
private fun dfs(obj: NestedInteger) {
275+
val action = obj.getInteger()
276+
if (action != null) {
277+
store.add(action)
278+
return
279+
}
280+
281+
obj.getList()?.forEach {
282+
dfs(it)
283+
}
284+
}
285+
286+
287+
fun next(): Int = store.removeFirst()
288+
289+
fun hasNext(): Boolean = store.isNotEmpty()
248290
}

0 commit comments

Comments
 (0)