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

LeetCode – Refresh – Combination Sum

时间:2015-03-19 06:17:15      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

This is a classical combination question. 

 

 1 class Solution {
 2 public:
 3     void getComb(vector<vector<int> > &result, const vector<int> &num, vector<int> current, int sum, int target, int start) {
 4         if (sum == target) {
 5             result.push_back(current);
 6             return;
 7         }
 8         if (sum > target) {
 9             return;
10         }
11         for (int i = start; i < num.size(); i++) {
12             current.push_back(num[i]);
13             getComb(result, num, current, sum + num[i], target, i);
14             current.pop_back();
15         }
16     }
17     vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
18         vector<vector<int> > result;
19         sort(candidates.begin(), candidates.end());
20         getComb(result, candidates, vector<int> (), 0, target, 0);
21         return result;
22     }
23 };

 

LeetCode – Refresh – Combination Sum

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4349268.html

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