标签:
1 class Solution { 2 public: 3 vector<vector<int>> combinationSum(vector<int>& candidates, int target) { 4 int size=candidates.size(); 5 vector<vector<int>> combination; 6 if(target==0) 7 return combination; 8 for(int i=size-1;i>=0;i--) 9 { 10 if(target>=candidates[i]) 11 { 12 int flag=0; 13 vector<int> newCan; 14 for(int j=0;j<=i;j++) 15 { 16 newCan.push_back(candidates[i]); 17 18 } 19 int can=candidates[i]; 20 int newTar=target-can; 21 if(newTar==0) 22 { 23 flag=1; 24 } 25 vector<vector<int>> res=combinationSum(newCan,newTar); 26 int sizeRes=res.size(); 27 if(size!=0||flag==1) 28 { 29 for(int j=0;j<=size;j++) 30 { 31 res[j].push_back(can); 32 } 33 combination.insert(combination.end(),res.begin(),res.end()); 34 } 35 36 } 37 38 } 39 return combination; 40 } 41 };
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/4631125.html