leetcode-39-组合总和

leetcode-39-组合总和

class Solution {

public:

    vector<vector<int>> res = {};

    void helper(vector<int>& candidates, int target, int begin, vector<int> curres){

        if (target == 0) res.push_back(curres);

        if (target > 0){

            for (int i = begin; i<candidates.size() && candidates[i] <= target; i++){

                curres.push_back(candidates[i]);

                helper(candidates, target-candidates[i], i, curres);

                curres.pop_back();

            }

        }

        return;

    }

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

        sort(candidates.begin(), candidates.end());

        helper(candidates, target, 0, vector<int>());

        return res;

    }

};