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
,
[7]
[2, 2, 3]
需要把子过程的结果存储起来
class Solution {public: void sub(vector & c,int len,int target , map,vector > > &m1){ if(m1.find(pair (len,target))!=m1.end()){ return; } else{ if(target==0){ m1[pair (len,target)]=vector >(1); return; } if(len==1){ if(target%c[0]==0){ vector >& vec=m1[pair (len,target)]; vec.resize(1); for(int j=0;j target){ sub(c,len-1,target,m1); m1[pair (len,target)]=m1[pair (len-1,target)]; return; } else{ sub(c,len-1,target,m1); m1[pair (len,target)]=m1[pair (len-1,target)]; vector >& vec=m1[pair (len,target)]; for(int i=1;i<=target/c[len-1];i++){ sub(c,len-1,target-i*c[len-1],m1); vector >& vec1=m1[pair (len-1,target-i*c[len-1])]; for(int j=0;j