*
* @param num int整型一维数组
* @param target int整型
* @return int整型二维数组
*/
function combinationSum2( num , target ) {
// write code here
if(num.length === 0) return [];
num.sort(function(a,b){return a - b}); //排序
if(num.start > num) return [];
let res = [],tmp = [];
dfs(num,target,res,tmp,0);
return res;
}
function dfs(num,target,res,tmp,start){
if(target === 0){
res.push(tmp.slice());//提取数组所有元素(浅拷贝)
return;
}
for(let i = start;i < num.length;i++){
if(target < num[i]) break;
if(i > start && num[i] === num[i - 1]) continue; //去重
tmp.push(num[i]);
dfs(num,target - num[i],res,tmp,i + 1);
tmp.pop();
}
}
module.exports = {
combinationSum2 : combinationSum2
};