ITPub博客

首页 > IT职业 > IT生活 > Lintcode135 Combination Sum solution 题解

Lintcode135 Combination Sum solution 题解

IT生活 作者:zrj1997 时间:2017-07-18 09:15:13 0 删除 编辑

【题目描述】

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和上一层相同

【参考答案】

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

转载自:简书网。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31475320/viewspace-2142205/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-07-12

  • 博文量
    9
  • 访问量
    2189