Combination Sum 待修改!

时间:2015-07-08 20:42:46   收藏:0   阅读:109
 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 };

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!