-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.cs
More file actions
85 lines (71 loc) · 2.58 KB
/
CombinationSum.cs
File metadata and controls
85 lines (71 loc) · 2.58 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCode.Arrays
{
[TestClass]
public class CombinationSum
{
public List<List<int>> combinationSum(int[] candidates, int target)
{
List<List<int>> result = new List<List<int>>();
if (candidates == null || candidates.Length == 0) return result;
List<int> current = new List<int>();
Array.Sort(candidates);
this.combinationSum(candidates, target, 0, current, result);
return result;
}
public void combinationSum(int[] candidates, int target, int j, List<int> curr, List<List<int>> result)
{
if (target == 0)
{
List<int> temp = new List<int>(curr);
result.Add(temp);
return;
}
for (int i = j; i < candidates.Length; i++)
{
if (target < candidates[i])
return;
curr.Add(candidates[i]);
combinationSum(candidates, target - candidates[i], i, curr, result);
curr.RemoveAt(curr.Count - 1);
}
}
public void TargetSumHelper(int[] clockNumbers, int target, int targetSum, List<List<int>> result, List<int> interimResult, int baseCounter, int counter)
{
}
public List<List<int>> TargetSum(int[] clockNumbers, int target)
{
List<List<int>> result = new List<List<int>>();
int start = 0;
while(start < clockNumbers.Length - 1)
{
List<int> intermediateResult = new List<int>();
int targetSum = 0;
for (int i=start; i< clockNumbers.Length; i++)
{
intermediateResult.Add(clockNumbers[i]);
targetSum += clockNumbers[i];
if (targetSum == target)
{
List<int> foundOne = new List<int>(intermediateResult);
result.Add(foundOne);
}
}
start++;
}
return result;
}
[TestMethod]
public void TestTargetClockSum()
{
int[] clockNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
// this.combinationSum(clockNumbers, 30);
List<List<int>> result = this.TargetSum(clockNumbers, 31);
}
}
}