forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
55 lines (51 loc) · 1.59 KB
/
Solution.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
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public IList<IList<int>> CombinationSum(int[] candidates, int target)
{
Array.Sort(candidates);
candidates = candidates.Distinct().ToArray();
var paths = new List<int>[target + 1];
paths[0] = new List<int>();
foreach (var c in candidates)
{
for (var j = c; j <= target; ++j)
{
if (paths[j - c] != null)
{
if (paths[j] == null)
{
paths[j] = new List<int>();
}
paths[j].Add(c);
}
}
}
var results = new List<IList<int>>();
if (paths[target] != null) GenerateResults(results, new Stack<int>(), paths, target, paths[target].Count - 1);
return results;
}
private void GenerateResults(IList<IList<int>> results, Stack<int> result, List<int>[] paths, int remaining,
int maxIndex)
{
if (remaining == 0)
{
results.Add(new List<int>(result));
return;
}
for (var i = maxIndex; i >= 0; --i)
{
var value = paths[remaining][i];
result.Push(value);
var nextMaxIndex = paths[remaining - value].BinarySearch(value);
if (nextMaxIndex < 0)
{
nextMaxIndex = ~nextMaxIndex - 1;
}
GenerateResults(results, result, paths, remaining - value, nextMaxIndex);
result.Pop();
}
}
}