码迷,mamicode.com
首页 > 其他好文 > 详细

Combination Sum

时间:2015-07-18 19:50:42      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

Combination Sum

标签:

原文地址:http://www.cnblogs.com/aguai1992/p/4657338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!