-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q341_FlattenNestedListIterator.cs
37 lines (32 loc) · 1.08 KB
/
Q341_FlattenNestedListIterator.cs
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
public class NestedIterator {
private readonly IList<int> integers;
private int index = -1;
public NestedIterator(IList<NestedInteger> nestedList)
{
this.integers = this.Fill(nestedList);
}
public IList<int> Fill(IList<NestedInteger> nestedList)
{
var result = new List<int>();
foreach (var nestedInteger in nestedList)
{
if (nestedInteger.IsInteger())
{
result.Add(nestedInteger.GetInteger());
}
else
{
result.AddRange(this.Fill(nestedInteger.GetList()));
}
}
return result;
}
public bool HasNext()
{
return this.index + 1 < this.integers.Count;
}
public int Next()
{
return this.integers[++this.index];
}
}