首页 > 其他 > 详细

Combination Sum

时间:2015-07-18 19:48:52      阅读:147      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!