# Leet code -- Combination Sum系列整理

proheart

## Combination Sum I

Given a set of candidate numbers (C) (without duplicates) 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.

``````[
[7],
[2, 2, 3]
]``````

``````public class CombinationSum {
public static List<List<Integer>> combinationSum(int[] candidates, int target){
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
}

private static void getResult(List<List<Integer>> result, ArrayList<Integer> current, int[] candidates, int target,
int start) {
if(target<0)    return;
if(target==0){
result.add(new ArrayList<>(current));
return;
}
for(int i = start; i<candidates.length && target >= candidates[i]; i++){
current.add(candidates[i]);
getResult(result, current, candidates, target-candidates[i], i);
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
int[] nums = {2,3,6,7};
System.out.println(combinationSum(nums, 7));
}
}``````

## LC40. Combination Sum II

``````[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]``````

``````/**
* 要去重，注意边界，递归时候要加一
*/
public class CombinationSum2 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
dfs(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
}

private void dfs(List<List<Integer>> result, ArrayList<Integer> current, int[] candidates, int target, int start) {
if(target < 0)  return;
if(target == 0){
result.add(new ArrayList<Integer>(current));
return;
}
for(int i = start; i<candidates.length && target >= candidates[i]; i++){
current.add(candidates[i]);
dfs(result, current, candidates, target - candidates[i], i+1);    // 此处注意i+1,每个元素只能用一次，加一后在向下递归
current.remove(current.size()-1);
while(i < candidates.length - 1 && candidates[i] == candidates[i+1]) i++;    // 去重复（注意上面有i+1,这里要length-1，边界问题）
}
}
public static void main(String[] args) {
int[] array = {10, 1, 2, 7, 6, 1, 5};
int target = 8;
System.out.println(new CombinationSum2().combinationSum2(array, target));
}
}``````

## LC216. Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1: Input: k = 3, n = 7

``````Output: [[1,2,4]]
``````

Example 2: Input: k = 3, n = 9

``````Output: [[1,2,6], [1,3,5], [2,3,4]]
``````

``````/**
* 注意结束条件：size达到k值 并且 剩余值为0
*/
public class CombinationSum3 {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> result = new ArrayList<>();
dfs(result, new ArrayList<Integer>(), k, n, 1);
return result;
}
private void dfs(List<List<Integer>> result, ArrayList<Integer> current, int k, int remainder, int start){
if(current.size() == k && remainder == 0){ //size达到k值 并且 剩余值为0
result.add(new ArrayList<>(current));
return ;
}
for(int i = start; i<=9 && remainder >= i; i++){
current.add(i);
dfs(result, current, k, remainder - i, i+1); // 不重复，i+1
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
System.out.println(new CombinationSum3().combinationSum3(3, 15));
}
}``````

## LC 377. Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

``````Example: nums = [1, 2, 3], target = 4
``````

The possible combination ways are:

``````(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
``````

Note that different sequences are counted as different combinations.
Therefore the output is 7.

Follow up: What if negative numbers are allowed in the given array?
How does it change the problem? What limitation we need to add to the
question to allow negative numbers?

``````public class CombinationSum4 {
public int combinationSum4(int[] candidates, int target){
List<List<Integer>> result = new ArrayList<>();
dfs(result, new ArrayList<Integer>(), candidates, target, 0);
return result.size();
}

private void dfs(List<List<Integer>> result, ArrayList<Integer> current, int[] candidates, int target, int start) {
if(target < 0)    return;
if(target == 0){
result.add(new ArrayList<>(current));
return;
}
for(int i = 0; i<candidates.length && target >= candidates[i]; i++){
current.add(candidates[i]);
dfs(result, current, candidates, target-candidates[i], i);
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
int[] arr = {1,2,3};
System.out.println(new CombinationSum4().combinationSum4(arr, 4));
}
}``````

``````public int combinationSum4(int[] candidates, int target){
Arrays.sort(candidates);
int[] dp = new int[target + 1];
dp[0] = 1;
for(int i = 1; i<dp.length; i++){
for(int curr: candidates){
if(curr > i)    break;
dp[i] += dp[i - curr];
}
}
return dp[target];
}``````

## 面试题：修改版

The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1)

``````public class CombinationSum {
public static List<List<Integer>> combinationSum(int[] candidates, int target){
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
}

private static void getResult(List<List<Integer>> result, ArrayList<Integer> current, int[] candidates, int target,
int start) {
if(target < 0)    return; // 是有可能小于0的
if(target == 0){
result.add(new ArrayList<>(current)); // 此处注意
return;
}
// 注意点1
for(int i = start; i<candidates.length && target >= candidates[i]; i++){
current.add(candidates[i]);
getResult(result, current, candidates, target-candidates[i], i); // 注意点2
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
int[] nums = {1,2,3};
System.out.println(combinationSum(nums, 4));
}
}``````

``````[[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2]]
``````

``````[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2, 1, 1], [2, 2], [3, 1]]
``````

``````[[1, 3]]
``````

##### 来刷算法
Learn data structure and algorithm using java.
##### proheart

Developer, Java & Android

38 声望
20 粉丝
0 条评论