陣列中相加之和等於特定值的元素
阿新 • • 發佈:2019-01-31
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
if (candidates == null)
return result;
Arrays.parallelSort(candidates);
return combinationSum(candidates, target, 0);
}
public List<List<Integer>> combinationSum(int[] candidates, int target, int start) {
List<List<Integer>> result = new ArrayList<>();
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1])
continue;
if (candidates[i] < target) {
for (List<Integer> list : combinationSum(candidates, target - candidates[i], i)) {
list.add(0 , candidates[i]);
result.add(list);
}
} else if (candidates[i] == target) {
List<Integer> res = new ArrayList<>();
res.add(candidates[i]);
result.add(res);
} else {
break;
}
}
return result;
}
public static void main(String[] args) {
int[] nums = {2, 2, 3, 7};
int target = 7;
System.out.println(new Solution().combinationSum(nums, target));
}
}