class Solution {
    // 1.回溯 因为是需要返回列表
    // 如果是求个数那就是完全背包
    // 递归三要素:1.返回值的类型和传递的参数 2.递归终止的条件  3.单层递归的逻辑
    List<List<Integer>> res = new ArrayList();
    List<Integer> path = new LinkedList();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates,target,0,0); // 后面俩个0,0 对应sum 和 startIndex 避免重复遍历
        return res;
    }
    public void backTracking(int[] candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            res.add(new ArrayList(path));
            return;
        }
        if (sum > target) {
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            sum += candidates[i];
            path.add(candidates[i]);
            backTracking(candidates,target,sum,i);
            sum -= candidates[i];
            path.removeLast();
        }
    }
    
}