Leetcode:Combination Sum

Description:

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

分析: 一个没有重复的集合,一个目标,怎么选取集合中的元素使其和为目标值,元素可以重复选取,这就是这道题的主要意思。这道和一个经典题目其实是

一个意思: 硬币总共有1,2,5,10,50的面值,问要凑出某某总额,有多少种方法? 这道题目更强,不仅要知道多少种,还要返回所有组合数目。

这道题目算是典型的深搜,先将集合排序,然后按照从小到大的元素来选取,当一个元素选取过了就不再选取了。 一个元素可以选取的次数从0到不大于目标值。

这道题目设置上要保证所有元素都大于0,因为如果有元素小于0,则无法设置终止条件,因为正元素就可以取无限次,因为有负数的存在!

 1 class Solution {
 2 public:
 3     vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
 4         sort(candidates.begin(),candidates.end());
 5         findcomb(candidates,target, 0);
 6         return result;
 7     }
 8     void findcomb(vector<int>& data, int target,int use)
 9     {
10         if(target==0)
11         {
12             result.push_back(onesample);
13             return;
14         }
15         if(use==data.size()) return;
16         
17         int nowvalue = data[use];
18         int time = target/nowvalue;
19         vector<int> record;
20         
21         for(int i=0;i<=time;i++)
22         {
23             record = onesample;
24             for(int j=1;j<=i;j++)
25             {
26                 onesample.push_back(nowvalue);
27             }
28             
29             findcomb(data,target-i*nowvalue,use+1);
30             onesample = record;
31         }
32     }
33     
34 private:
35     vector<int> onesample;
36     vector<vector<int> >result;
37 };

 

 

posted on 2014-06-18 21:36  soyscut  阅读(337)  评论(0编辑  收藏  举报

导航