# Combination Sum 组合数求和-Leetcode

## 原题：

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.

Note:

• 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.

For example, given candidate set `2,3,6,7` and target `7`,  A solution set is:  `[7]` `[2, 2, 3]`

```public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
return ans;
}
private  void solve(int[] candidates,int start,int target,LinkedList<Integer> current){
if (target==0){
return;
}
if (target<candidates[start]){
return;
}
else {
for (int iterator=start;iterator<candidates.length;iterator++){
if (candidates[iterator]>target){
break;
}
solve(candidates, iterator, target - candidates[iterator], current);
current.pollLast();
}
return;
}
}
for (int i:source){
}
return dest;
}
}```

```public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
return combinationSum(candidates, target, 0);
}

public List<List<Integer>> combinationSum(int[] candidates, int target, int start) {

List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
for (int i = start; i < candidates.length; i++) {
if (candidates[i] <target) {
for (List<Integer> ar : combinationSum(candidates, target - candidates[i], i)) {
}
} else if (candidates[i] == target) {
List<Integer> lst = new ArrayList<>();
} else
break;
}
return res;
}
}```

74 篇文章40 人订阅

0 条评论

## 相关文章

13120

35560

65090

16820

22080

19420

43320

56820

### PHP数据结构（六） ——树与二叉树之概念及存储结构

PHP数据结构（六）——树与二叉树之概念及存储结构 （原创内容，转载请注明来源，谢谢） 一、树的含义 1、树为非线性结构，是n(n>=0)个节点的有限集，非空树...

544100