forked from zzxboy1/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination_Sum_II.js
More file actions
29 lines (29 loc) · 881 Bytes
/
Copy pathCombination_Sum_II.js
File metadata and controls
29 lines (29 loc) · 881 Bytes
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
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
candidates.sort(function(a,b){
return a-b;
});
var result = [],
tempSave = [];
var find = function(target, temp, index,temp2) {
for (var i = index; i <candidates.length; i++) {
if (candidates[i] > target) {
break;
} else if (candidates[i] === target) {
if(tempSave[temp2+candidates[i]]){
break;
}
result.push(temp.concat([candidates[i]]));
tempSave[temp2+candidates[i]]=true;
} else {
find(target - candidates[i], temp.concat([candidates[i]]), i+1,temp2+candidates[i]);
}
}
};
find(target, [], 0,"");
return result;
};