这道题做成这样子。第二个错误忘加了,也就罢了,第一个是怎么想的,在list里加sum做啥的。昨晚糊涂了,怎么刚才也没看出来。这是怎么了,再痛苦,委顿也不是这样的吧!!!!!!
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new LinkedList<>();
if (candidates == null || candidates.length == 0) {
return result;
}
Arrays.sort(candidates);
List<Integer> list = new LinkedList<>();
combinationSumHelper(result, list, candidates, target, 0, 0);
return result;
}
private void combinationSumHelper(List<List<Integer>> result, List<Integer> list, int[] candidates, int target, int sum, int j) {
if (target == sum) {
result.add(new LinkedList<>(list));
return;
}
for (int i = j; i < candidates.length; i++) {
if (sum + candidates[i] > target) {
return;
}
//sum = sum + candidates[i];
//1 list.add(sum);
list.add(candidates[i]);
//combinationSumHelper(result, list, candidates, target, sum, i);
combinationSumHelper(result, list, candidates, target, sum + candidates[i], i);
list.remove(list.size() - 1);
///
//2 sum = sum - candidates[i];
///
}
}
}