public class _39组合总和 {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
LinkedList<Integer> track = new LinkedList<>();
backTrack(track, candidates, target, 0, 0);
return result;
}
private void backTrack(LinkedList<Integer> track, int[] candidates, int target, int cur, int start) {
if (cur > target) {
return;
}
if (cur == target) {
result.add(new ArrayList<>(track));
return;
}
for (int i = start; i < candidates.length; i++) {
track.add(candidates[i]);
backTrack(track, candidates, target, cur + candidates[i], i);
track.removeLast();
}
}
}