-
Notifications
You must be signed in to change notification settings - Fork 30
/
CombinationSum.cs
executable file
·73 lines (69 loc) · 3.98 KB
/
CombinationSum.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
66
67
68
69
70
71
72
// Source : https://leetcode.com/problems/combination-sum/
// Author : codeyu
// Date : Tuesday, October 25, 2016 11:14:47 PM
/**********************************************************************************
*
*
* Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
*
*
* The same repeated number may be chosen from C unlimited number of times.
*
*
* Note:
*
* All numbers (including target) will be positive ints.
* The solution set must not contain duplicate combinations.
*
*
*
*
* For example, given candidate set [2, 3, 6, 7] and target 7,
* A solution set is:
*
* [
* [7],
* [2, 2, 3]
* ]
*
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
using System.Linq;
namespace Algorithms
{
public class Solution039
{
public static IList<IList<int>> CombinationSum(int[] candidates, int target)
{
IList<IList<int>> result = new List<IList<int>>();
if(candidates == null || candidates.Length==0)
return result;
Array.Sort(candidates);
helper(candidates, 0, target, new List<int>(), result);
return result;
}
private static void helper(int[] candidates, int start, int target, IList<int> item,
IList<IList<int>> result)
{
if(target<0)
return;
if(target==0)
{
result.Add(new List<int>(item));
return;
}
for(int i=start;i<candidates.Length;i++)
{
if(i>0 && candidates[i]==candidates[i-1])
continue;
item.Add(candidates[i]);
helper(candidates,i,target-candidates[i],item,result);
item.RemoveAt(item.Count-1);
}
}
}
}