-
Notifications
You must be signed in to change notification settings - Fork 20
/
CombinationSumII.js
103 lines (90 loc) · 2.32 KB
/
CombinationSumII.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* 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]
* ]
*
* Accepted.
*/
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
let combinationSum2 = function (candidates, target) {
if (candidates.length === 0) {
return [];
}
let lists = [];
candidates.sort();
dfs(candidates, target, [], lists, 0);
return lists;
};
let dfs = function (candidates, target, path, ret, index) {
if (target < 0) {
return;
}
if (target === 0) {
let tmp = [];
path.forEach(function (i) {
tmp.push(i);
});
ret.push(tmp);
return;
}
for (let i = index; i < candidates.length; i++) {
if (i !== index && candidates[i] === candidates[i - 1]) {
continue;
}
path.push(candidates[i]);
dfs(candidates, target - candidates[i], path, ret, i + 1);
path.splice(path.length - 1, 1);
}
};
// Expected: [[1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]
let list0 = combinationSum2([10, 1, 2, 7, 6, 1, 5], 8);
let set = new Set([[1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]);
if (list0.length === 4) {
console.log("pass")
} else {
console.error("failed")
}
if (new Set(list0).toString() === set.toString()) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: []
if (combinationSum2([1], 2).length === 0) {
console.log("pass")
} else {
console.error("failed")
}
// Expected: [[1, 1, 2], [2, 2]]
set.clear();
let list1 = combinationSum2([1, 1, 2, 2], 4);
set.add([[1, 1, 2], [2, 2]]);
if (list1.length === 2) {
console.log("pass")
} else {
console.error("failed")
}
if (new Set(list1).toString() === set.toString()) {
console.log("pass")
} else {
console.error("failed")
}
console.log(combinationSum2([5, 4, 5, 1, 5, 3, 1, 4, 5, 5, 4], 10));
console.log(combinationSum2([3, 5, 4, 1, 2, 2, 4, 2, 2, 1, 5], 2));