美文网首页工作生活
leecode 40:组合总和||

leecode 40:组合总和||

作者: 小强不是蟑螂啊 | 来源:发表于2019-07-03 19:15 被阅读0次

题目:
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

var combinationSum2 = function(arr, target) {
    arr.sort(function(a,b){return a-b});
    var len = arr.length;
    dp = [];
    for(var i=0;i<=target;i++){
        dp[i] = new Set();
    }
    dp[0].add('');
    for(let c of arr){
        for(var j=target;j>=c;j--){
            for(let item of dp[j-c]){
                dp[j].add(item+','+c);
            }
        }
    }
    return Array.from(dp[target]).map((item)=>{
        return item.slice(1).split(',');
    })
};

相关文章

  • leecode 40:组合总和||

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数...

  • 40 组合总和 II

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使...

  • 40. 组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为...

  • 40.组合总和II

    题目给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字...

  • 40.组合总和2

  • [LeetCode]40、组合总和2

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以...

  • 40.组合总和 II

    自己解法 标准的回溯加剪枝,思路很清晰,但是在细节上出了点小问题,一个是递归的时候应该用i+1,用成了start ...

  • 40. 组合总和 II

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以...

  • 40. 组合总和 II

    题目 40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candid...

  • LeetCode - #40 组合总和 II

    前言 我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤...

网友评论

    本文标题:leecode 40:组合总和||

    本文链接:https://www.haomeiwen.com/subject/ywachctx.html