leetcode 39. 组合总和

leetcode 39. 组合总和_第1张图片

         经典回溯题,有1个关键点:

  • 同一个元素可以无限制重复被选取,一会代码中的start_index就不用+1了。

        其余的都按经典回溯做法做就行了,看代码:

class Solution {
public:
    vector> ans;
    vector path;
    void backtrating(vector candidates,int target,int sum,int start_index)
    {
        if(sum == target)
        {
            ans.push_back(path);
            return;
        }
        if(sum > target) return;
        for(int i=start_index; i> combinationSum(vector& candidates, int target) {
        backtrating(candidates,target,0,0);
        return ans;
    }
};

你可能感兴趣的:(leetcode专栏,leetcode,算法,职场和发展,c++,数据结构)