-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution341.cs
53 lines (46 loc) · 1.22 KB
/
Solution341.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution341
{
public interface NestedInteger
{
bool IsInteger();
int GetInteger();
IList<NestedInteger> GetList();
}
/// <summary>
/// 341. Flatten Nested List Iterator - Medium
/// <a href="https://leetcode.com/problems/flatten-nested-list-iterator">See the problem</a>
/// </summary>
public class NestedIterator
{
private readonly List<int> _list = [];
private int _index = 0;
public NestedIterator(IList<NestedInteger> nestedList)
{
Flatten(nestedList);
}
private void Flatten(IList<NestedInteger> nestedList)
{
foreach (var nestedInteger in nestedList)
{
if (nestedInteger.IsInteger())
{
_list.Add(nestedInteger.GetInteger());
}
else
{
Flatten(nestedInteger.GetList());
}
}
}
public bool HasNext()
{
return _index < _list.Count;
}
public int Next()
{
return _list[_index++];
}
}
}