# [Lintcode] Combination Sum

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.

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

##### Notice
• All numbers (including target) will be positive integers.
• Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
• The solution set must not contain duplicate combinations.
Example

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

public class Solution {
/**
* @param candidates: A list of integers
* @param target:An integer
* @return: A list of lists of integers
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(res, new ArrayList<Integer>(), candidates, target, 0);
return res;
}
void helper(List<List<Integer>> res, List<Integer> list, int[] candidates, int target, int index) {
if(target == 0 && !res.contains(list)) {
return;
}
if(index >= candidates.length || target < 0)
return;

for(int i = index; i < candidates.length; i++) {
helper(res, list, candidates, target - candidates[i], i);
list.remove(list.size() - 1);
}
}
}

• 本文已收录于以下专栏：

## Leetcode|Combination Sum III[回溯]

Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

## LeetCode_40---Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

## Combination Sum

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

## leetcode【39+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合，使得组合中数字的和是target值。并且组合里的数字可以重复，也就是不限制C中每一个数字的使用。 这明显是回溯啦，大家记得让...

## 【LeetCode】 Combination Sum 系列

Combination Sum 【LeetCode】39.Combination Sum 【LeetCode】40.Combination Sum2

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)