Given a set of candidate numbers (candidates
) (without duplicates)and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
The same repeated number may be chosen from candidates
unlimited number of times.
Note:
target
) will be positive integers.Example 1:
Input: candidates = [2,3,6,7],
target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5],
target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Accepted 394,084 Submissions 777,329
medium 级别难度,这是一道经典题目。求数组里面能组合成target的数字所有组合。它允许结果出现重复数字。
我做题还处于入门摸索阶段,但是如果做完一贷题目,后面提示你有 类似这种:Combination Sum IICombinationsCombination Sum IIIFactor CombinationsCombination Sum IV。就是说明它有体系,成套路的衍生出相关的题目。值得去做。
这种问题的思路,跟之前做的数独题优点类似,尝试去匹配,如果满足条件,就下一步,不满足就回退重新选择。
因此题目的关键就转变成递归函数的实现。图片来自花花酱大神,主要体现了DFS就是深度优先的思路。https://zxi.mytechroad.com/blog/searching/leetcode-39-combination-sum/
思路:
1.先排序 ,防止乱序导致重复搜索。
2. 递归参数:增加index(根据题目要求必须遍历所有可能性才能求解。为了避免重复遍历,我们搜索的时候只搜索当前或之后的数,而不再搜索前面的数),临时结果list, 最终结果res,还有临时sum。
如果 临时sum》target: 不满足,返回。
如果 临时sum == target,匹配,加入结果。
如果 临时sum Runtime: 7 ms, faster than 19.80% of Java online submissions forCombination Sum. Memory Usage: 37.9 MB, less than 100.00% of Java online submissions for Combination Sum. 时间复杂度O(N!) 如归按照大神的写法,如下所示,直接使用target-arrays[i],匹配的时候判断target==0.就避免了多传临时结果,求和。速度更快了。 Runtime: 2 ms, faster than 99.82% of Java online submissions forCombination Sum. Memory Usage: 37.3 MB, less than 100.00% of Java online submissions for Combination Sum.public static void main(String[] args) {
// TODO Auto-generated method stub
int[] candidates ={2,3,6,7};
List res = combinationSum(candidates,7);
System.out.println(res);
}
public static List
> combinationSum(int[] candidates, int target) {
List
> res = new ArrayList();
List
> result,int tempsum){
//超范围,不匹配
if(tempsum> target){
return;
}//匹配,加入结果集
else if(target ==tempsum){
result.add(new ArrayList( temp));
}else{
//
private void combin(int[] arrays,int target,int index,List
> result){
//超范围,不匹配
if( target<0){
return;
}//匹配,加入结果集
else if(target ==0){
result.add(new ArrayList( temp));
}else{
//