Given an array candidates without repeating elements and a target number target,
Find out all the combinations of numbers and target s in candidates.
The numbers in candidates can be selected repeatedly without limit.
Explain:
All numbers, including target, are positive integers.
A solution set cannot contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
The solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
The solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Source: LeetCode
Link: https://leetcode-cn.com/problems/combination-sum
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.
package com.leetcode.array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Title:
* Combined total -- leetcode 39
*
* Title Description:
*
Given an array candidates without repeating elements and a target number target,
Find out all the combinations of numbers and target s in candidates.
candidates The number in can be selected repeatedly without limit.
Explain:
All numbers, including target, are positive integers.
A solution set cannot contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
The solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
The solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Source: LeetCode
Link: https://leetcode-cn.com/problems/combination-sum
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.
*/
public class CombinationSum {
/**
* Train of thought:
* 1,Backtracking algorithm
* 2,Recursively find and combine target, exit and exceed target
*/
public List<List<Integer>> combinationSum(int[] bb, int target) {
List<List<Integer>> res = new ArrayList<>();
if (bb == null) {
return res;
}
addCombinations(bb, 0, target, new ArrayList<Integer>(), res);
return res;
}
private void addCombinations(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) {
if (target < 0) {
return;
}
if (target == 0) {
res.add(new ArrayList<>(cache));
return;
}
for (int i=start; i<bb.length; i++) {
cache.add(bb[i]);
addCombinations(bb,i,target-bb[i],cache,res);
cache.remove(cache.size()-1);
}
}
/**
* Train of thought:
* Backtracking after optimization
*/
public List<List<Integer>> combinationSumII(int[] bb, int target) {
List<List<Integer>> res = new ArrayList<>();
if (bb == null) {
return res;
}
// After sorting the array, the number of recursions can be reduced during recursion, and if (BB [i] > target) break can be used.
Arrays.sort(bb);
addCombinationsII(bb, 0, target, new ArrayList<Integer>(), res);
return res;
}
private void addCombinationsII(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) {
if (target < 0) {
return;
}
if (target == 0) {
res.add(new ArrayList<>(cache));
return;
}
for (int i=start; i<bb.length; i++) {
// Improve performance with sorted array
if (bb[i] > target) {
break;
}
cache.add(bb[i]);
addCombinationsII(bb,i,target-bb[i],cache,res);
cache.remove(cache.size()-1);
}
}
}