【题目描述】
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.
Notice
·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.
给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T。C中的数字可以无限制重复被选取。
例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为:[7],[2,2,3]
【注】:
1、所有的数字(包括目标数字)均为正整数。
2、元素组合(a1,a2, … ,ak)必须是非降序(ie,a1≤a2≤ … ≤ak)。
3、解集不能包含重复的组合。
【题目链接】
www.lintcode.com/en/problem/combination-sum/
【题目解析】
与Subsets类似。注意几点:
1) 每加入一个数,就要把下一层的targte为原来的targte减去这个数
2) 需要考虑避免重复:对元素排序,判断是否和前一个数等值
3) 可以重复取相同元素,所以下一层的起始pos和上一层相同
【参考答案】