力扣39题---方法:回溯

39. 组合总和

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。

说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。

示例 1:
输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]

示例 2
输入:candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

回溯算法:一条路走到底,如果得到的结果不对则返回到上一步,选择另外几个方向走到底,遇到对的结果或确定接下来的路都错误或者本步所有的路都走完,返回到上一步进行循环。

在这里插入图片描述

class Solution39:
    def combinationSum(self, candidates: list(), target: int):

        def dfs(candidates, begin, size, path, res, target):
            if target < 0:
                return
            if target == 0:
                res.append(path)
                return

            for index in range(begin, size):
                dfs(candidates, index, size, path + [candidates[index]], res, target - candidates[index])


        size = len(candidates)
        if size == 0:
            return []
        path = []
        res = []
        dfs(candidates, 0, size, path, res, target)
        return res
class Solution {
public:        // 2 3 6 7       7
    vector<vector<int>> combinationSum(vector<int>& candidates, int target)
    {
        int size = candidates.size();
        vector<vector<int>>res;
        vector<int>path;
        sort(candidates.begin(),candidates.end());
        Calu(candidates,target,0,size,res,path);
        return res;
    }
    void Calu(vector<int>&cand,int target,int begin,int size,vector<vector<int>>&res,vector<int>&path)
    {

        if(target <0)
        {
             return;
        }
        if(target == 0)
        {
            res.push_back(path);
            cout<<"res = ";
            for(int i = 0;i<res.size();i++)
            {
                for(int j = 0;j<res[i].size();j++)
                cout<<res[i][j];
            }
            cout<<endl;
            return;
        }

        for(int index =begin;index<size;index++)
        {


            path.push_back(cand[index]);
            cout<<"path = ";
            for(int i = 0;i<path.size();i++)
            {
                cout<<path[i];
            }
            cout<<endl;

            //Calu执行完,就表示有target<=0  总是需要剔除最后一个元素。
            Calu(cand,target - cand[index],index,size,res,path);
            path.pop_back();                                   //这句语句执行时,说明上一句Calu已经执行完了 ,当target<=0时才会执行完 所以需要把最后一个元素剔除,准备放入下一个元素
            if(target - cand[index] <=0)    //出现最新target<=0时,本层循环终止
            {
                 break;                //当出现最新target小于0时,本次循环终止,直接进入上一层循环。
            }
        }
    }
};

回溯算法的核心:

 path.push_back(cand[index]);          							//插入
 Calu(cand,target - cand[index],index,size,res,path);          	//判断是否继续插入,内部实现正确保存,错误返回。
  path.pop_back();                                            	//撤销

每条路径都尝试并且都将路径保存,符合返回上一路经的条件时,返回到上一路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值