Lintcode135 Combination Sum solution 题解

【题目描述】

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

The same repeated number may be chosen from 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和上一层相同

【参考答案】

www.jiuzhang.com/solutions/combination-sum/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容