-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCombinationSumII.cs
executable file
·73 lines (69 loc) · 4.11 KB
/
CombinationSumII.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-ii/
// Author : codeyu
// Date : Tuesday, October 25, 2016 11:15:50 PM
/**********************************************************************************
*
*
* Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
*
*
* Each number in C may only be used once in the combination.
*
* Note:
*
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
*
*
*
*
* For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
* A solution set is:
*
* [
* [1, 7],
* [1, 2, 5],
* [2, 6],
* [1, 1, 6]
* ]
*
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution040
{
public static IList<IList<int>> CombinationSum2(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 || start >= candidates.Length)
return;
if(target==0)
{
result.Add(new List<int>(item));
return;
}
for(int i=start; i<candidates.Length; i++)
{
if(i > start && candidates[i] == candidates[i-1])
continue;
item.Add(candidates[i]);
helper(candidates, i+1, target-candidates[i], item, result);
item.RemoveAt(item.Count-1);
}
}
}
}