classSolution { private List<List<Integer>> res = newArrayList<>(); private LinkedList<Integer> list = newLinkedList<>(); privateintsum=0; public List<List<Integer>> combinationSum(int[] candidates, int target) { combina(0,candidates,target); return res; } publicvoidcombina(int start, int[] candidates, int target){ if (sum==target){ res.add(newLinkedList<>(list)); return; } if (sum>target) return; for (inti= start;i<candidates.length;i++){ //选择 list.add(candidates[i]); sum = sum + candidates[i]; combina(i,candidates,target); //撤销选择 list.removeLast(); sum = sum - candidates[i]; } } }