Combined sum
Title description: Given an array of candidates with no duplicate elements and a target number target, find out all combinations of candidates that can make the sum of the numbers target.
The numbers in candidates can be repeatedly selected without limitation.
illustrate:
- All numbers (including target) are positive integers.
- The solution set cannot contain repeated combinations.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/combination-sum/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Exhaustive method
Similar to construct a multi-branch tree, the maximum depth is the length of the candidates array, and then all possible paths are obtained. The maximum path is from the root node to the leaf node. Determine whether the sum of all paths is equal to the target. If they are equal, add to the result Concentrate, and finally need to be judged heavy, remove the repeated combination, and finally return.
import java.util.*;
public class LeetCode_039 {
/**
* 穷举法
*
* @param candidates
* @param target
* @return
*/
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
// 结果集
List<List<Integer>> result = new ArrayList<>();
// 所有可能的组合情况
Queue<List<Integer>> allPossibleCombinations = new LinkedList<>();
// 初始化所有的情况
for (int candidate : candidates) {
List<Integer> onePossibleCombination = new ArrayList<>();
onePossibleCombination.add(candidate);
allPossibleCombinations.add(onePossibleCombination);
}
while (!allPossibleCombinations.isEmpty()) {
List<Integer> temp = allPossibleCombinations.poll();
int sum = 0;
for (Integer num : temp) {
sum += num;
}
if (sum == target) {
result.add(temp);
} else if (sum < target) {
for (int candidate : candidates) {
// List复制方法
List<Integer> toAdd = new ArrayList<>(Arrays.asList(new Integer[temp.size()]));
Collections.copy(toAdd, temp);
toAdd.add(candidate);
allPossibleCombinations.add(toAdd);
}
}
}
// 去重后的结果
List<List<Integer>> result1 = new ArrayList<>();
for (int i = 0; i < result.size(); i++) {
boolean isRepeated = false;
List<Integer> one = result.get(i);
Collections.sort(one);
for (int j = i + 1; j < result.size(); j++) {
List<Integer> two = result.get(j);
Collections.sort(two);
if (one.size() != two.size()) {
continue;
}
boolean equals = true;
for (int x = 0; x < one.size(); x++) {
if (!one.get(x).equals(two.get(x))) {
equals = false;
continue;
}
}
if (equals) {
isRepeated = true;
}
}
if (!isRepeated) {
result1.add(one);
}
}
return result1;
}
public static void main(String[] args) {
int[] candidates = new int[]{8, 10, 6, 3, 4, 12, 11, 5, 9};
for (List<Integer> integers : combinationSum(candidates, 28)) {
for (Integer integer : integers) {
System.out.print(integer + " ");
}
System.out.println();
}
}
}
[Daily Message] Don't rush to let life give all the answers, sometimes we need to wait patiently. Believe in the process, move forward calmly, live up to life, and life will live up to you.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。