-
Notifications
You must be signed in to change notification settings - Fork 130
/
Combination Sum II.js
59 lines (49 loc) · 1.44 KB
/
Combination Sum II.js
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
/**
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]
*/
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
var len = candidates.length,
result = [],
i,
j;
candidates.sort(function(a, b) {
return a - b;
});
genSum(result, target, [], 0, 0, candidates);
return result;
};
function genSum(result, target, curArr, index, curSum, nums) {
if (curSum === target) {
result.push(curArr);
return;
}
if (curSum > target || index === nums.length) {
return;
}
var len = nums.length,
i;
for (i = index; i < len; i++) {
if (i > index && (nums[i] === nums[i - 1])) {
continue;
}
curArr.push(nums[i]);
genSum(result, target, curArr.concat(), i + 1, curSum + nums[i], nums);
curArr.pop();
}
}