LeeCode 39. 组合总和

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 16:41   198   0
var combinationSum = function(candidates, target) {
    const len = candidates.length;
    const ret = [];
    const dfs = (start, buf=[],target) => {
        for(let index = start;index < len;index++){
            buf.push(candidates[index]);
        
            if(candidates[index] === target ) {
                ret.push([...buf])
            } 
            else if (candidates[index] < target ) {
                dfs(index, buf,(target - candidates[index]))
                           
            }
            buf.pop();
        }
    }
    dfs(0,[],target);
    return ret;
};

用了回溯,剪枝这块想了好久,一年前还写过~用index去控制循环,之前想的是去判断每次压入的要比之前的数值大~

https://leetcode-cn.com/problems/combination-sum/

题目描述这里

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP