标签:
https://leetcode.com/problems/combination-sum/
1 class Solution { 2 public: 3 void getResult(vector<vector<int>>& res,vector<int>& path,int target,int index,int sum,vector<int>& candidates) 4 { 5 if(sum>target) 6 { 7 return; 8 } 9 else if(sum==target) 10 { 11 res.push_back(path); 12 return ; 13 } 14 else 15 { 16 for(int i=index;i<candidates.size();i++) 17 { 18 path.push_back(candidates.at(i)); 19 getResult(res,path,target,i,sum+candidates.at(i),candidates); 20 path.pop_back(); 21 } 22 } 23 24 } 25 vector<vector<int>> combinationSum(vector<int>& candidates, int target) { 26 int size=candidates.size(); 27 vector<vector<int>> res; 28 if(target==0) 29 return res; 30 sort(candidates.begin(),candidates.end()); 31 vector<int> path; 32 33 getResult(res,path,target,0,0,candidates); 34 35 return res; 36 37 } 38 };
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/4657338.html