Lintcode135 Combination Sum solution 题解

针对给定的一组候选数字和目标数字,寻找所有可能的组合,使得这些组合的数字之和等于目标数字。该问题要求元素组合必须是非降序排列,并且解集中不能包含重复的组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目描述】

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/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值