//经典回溯法
class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> res; sort(candidates.begin(),candidates.end()); vector<int> add; DFS(candidates,target,0,add,res); return res; } void DFS(vector<int>& candidates, int target,int start,vector<int>& add,vector<vector<int>>& res){ if(target < 0)return; else if(target == 0){res.push_back(add);} else{ for(int i=start;i < candidates.size();i++){ add.push_back(candidates[i]); DFS(candidates,target-candidates[i],i,add,res); add.pop_back(); } } } };