forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlattenNestedListIterator.swift
95 lines (81 loc) · 2.76 KB
/
FlattenNestedListIterator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Question Link: https://leetcode.com/problems/flatten-nested-list-iterator/
* Primary idea I: Use a stack to store nested list reversely.
* Every time pop a element and push back until it is an integer.
* Time Complexity I: next - O(1), hasNext - O(n), Space Complexity I: O(n)
*
* Primary idea II: Flatten the nested list at very first.
* Time Complexity II: next - O(1), hasNext - O(1), Space Complexity II: O(n)
*
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* public func isInteger() -> Bool
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* public func getInteger() -> Int
*
* // Set this NestedInteger to hold a single integer.
* public func setInteger(value: Int)
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public func add(elem: NestedInteger)
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* public func getList() -> [NestedInteger]
* }
*/
// Option I
class NestedIteratorI {
private var stack: [NestedInteger]
init(_ nestedList: [NestedInteger]) {
stack = nestedList.reversed()
}
func next() -> Int {
return stack.removeLast().getInteger()
}
func hasNext() -> Bool {
while !stack.isEmpty {
var nestedInteger = stack.removeLast()
if nestedInteger.isInteger() {
stack.append(nestedInteger)
return true
} else {
for nestedInt in nestedInteger.getList().reversed() {
stack.append(nestedInt)
}
}
}
return false
}
}
// Option II
class NestedIteratorII {
private var list: [Int]
private var index: Int
init(_ nestedList: [NestedInteger]) {
list = [Int]()
index = 0
flatten(nestedList)
}
func next() -> Int {
let current = list[index]
index += 1
return current
}
func hasNext() -> Bool {
return index < list.count
}
private func flatten(_ nestedList: [NestedInteger]) {
for nestedInt in nestedList {
if nestedInt.isInteger() {
list.append(nestedInt.getInteger())
} else {
flatten(nestedInt.getList())
}
}
}
}