码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-----39. 组合总和

时间:2020-07-06 16:29:07      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:ble   int   tor   ati   pre   com   pop   cto   ems   

代码(dfs)

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        dfs(candidates, 0, target);
        return ans;    
    }

    void dfs(vector<int> cs, int u, int target) {
        if (target == 0) {
            ans.push_back(path);
            return;
        }
        if (u == cs.size()) return;

        for (int i = 0; cs[u] * i <= target; ++i) {
            dfs(cs, u + 1, target - cs[u] * i);
            path.push_back(cs[u]);
        }
        for (int i = 0; cs[u] * i <= target; ++i) {
            path.pop_back();
        }
    }
};

leetcode-----39. 组合总和

标签:ble   int   tor   ati   pre   com   pop   cto   ems   

原文地址:https://www.cnblogs.com/clown9804/p/13254932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!