-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0216-CombinationSumIII.cs
39 lines (35 loc) · 1.18 KB
/
0216-CombinationSumIII.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
//-----------------------------------------------------------------------------
// Runtime: 200ms
// Memory Usage: 25.8 MB
// Link: https://leetcode.com/submissions/detail/394731091/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0216_CombinationSumIII
{
public IList<IList<int>> CombinationSum3(int k, int n)
{
var results = new List<IList<int>>();
Helper(0, k, n, new List<int>(), results);
return results;
}
private void Helper(int index, int k, int n, IList<int> current, IList<IList<int>> results)
{
if (current.Count == k && n == 0)
{
results.Add(current);
return;
}
if (index >= k || n < 0) return;
var start = index == 0 ? 1 : current.Last() + 1;
for (int i = start; i <= n && i < 10; i++)
{
var newList = new List<int>(current);
newList.Add(i);
Helper(index + 1, k, n - i, newList, results);
}
}
}
}