-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1286.iterator-for-combination.cs
58 lines (49 loc) · 1.28 KB
/
1286.iterator-for-combination.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
54
55
56
57
/*
* @lc app=leetcode id=1286 lang=csharp
*
* [1286] Iterator for Combination
*/
// @lc code=start
public class CombinationIterator
{
IList<string> ans = new List<string>();
IList<char> temp = new List<char>();
int idx = 0;
public CombinationIterator(string characters, int combinationLength)
{
go(characters, combinationLength, 0);
}
public void go(string characters, int combinationLength, int len)
{
if (combinationLength == temp.Count)
{
string str = new string(temp.ToArray());
ans.Add(str);
return;
}
for (int i = len; i < characters.Length; i++)
{
if (temp.Count + 1 <= combinationLength)
{
temp.Add(characters[i]);
go(characters, combinationLength, i + 1);
temp.RemoveAt(temp.Count - 1);
}
}
}
public string Next()
{
return ans[idx++];
}
public bool HasNext()
{
return idx < ans.Count;
}
}
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator obj = new CombinationIterator(characters, combinationLength);
* string param_1 = obj.Next();
* bool param_2 = obj.HasNext();
*/
// @lc code=end