Skip to content

Commit 4a252c6

Browse files
committed
Time: 221 ms (35%), Space: 40.5 MB (40%) - LeetHub
1 parent 0e28f0c commit 4a252c6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class NestedIterator(nestedList: List<NestedInteger>) {
2+
private val flat: List<Int> = nestedList.flatten()
3+
private val iterator = flat.iterator()
4+
5+
private fun List<NestedInteger>.flatten(): List<Int> =
6+
flatMap {
7+
if (it.isInteger())
8+
listOf(it.getInteger()!!)
9+
else
10+
it.getList()!!.flatten()
11+
}
12+
13+
fun next(): Int {
14+
return iterator.next()
15+
}
16+
17+
fun hasNext(): Boolean {
18+
return iterator.hasNext()
19+
}
20+
}

0 commit comments

Comments
 (0)