-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1286-IteratorForCombination.cs
65 lines (55 loc) · 1.69 KB
/
1286-IteratorForCombination.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
58
59
60
61
62
63
64
65
//-----------------------------------------------------------------------------
// Runtime: 120ms
// Memory Usage: 31.6 MB
// Link: https://leetcode.com/submissions/detail/363007096/
//-----------------------------------------------------------------------------
using System.Text;
namespace LeetCode
{
public class _1286_IteratorForCombination
{
private readonly int[] nums;
private readonly string str;
private readonly int n;
private readonly int k;
private bool hasNext;
public _1286_IteratorForCombination(string characters, int combinationLength)
{
str = characters;
n = characters.Length;
k = combinationLength;
hasNext = true;
nums = new int[k];
for (int i = 0; i < k; i++)
nums[i] = i;
}
public string Next()
{
var sb = new StringBuilder();
foreach (var index in nums)
sb.Append(str[index]);
var j = k - 1;
while (j >= 0 && nums[j] == n - k + j)
j--;
if (j >= 0)
{
nums[j]++;
for (int i = j; i < k; i++)
nums[i] = nums[j] + i - j;
}
else
hasNext = false;
return sb.ToString();
}
public bool HasNext()
{
return hasNext;
}
}
/**
* 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();
*/
}