给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
    candidates 中的数字可以无限制重复被选取。
    说明:
    所有数字(包括 target)都是正整数。
    解集不能包含重复的组合。
    示例 1:
    输入:candidates = [2,3,6,7], target = 7,
    所求解集为:
    [
    [7],
    [2,2,3]
    ]
    示例 2:
    输入:candidates = [2,3,5], target = 8,
    所求解集为:
    [
    [2,2,2,2],
    [2,3,3],
    [3,5]
    ]
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/combination-sum


    思路:
    已经刷了好多DFS的题,都是差不多的,手感越来越好
    第一步:这题没有什么特殊情况排除
    第二步:建立基础遍历,一个记录最终的返回值,一个记录当前的路径上的值,一个记录判断到第几个值了
    第三步:dfs每次向下都是用给定的值减去给定数组中的值,可以重复利用,所以不同判断是否已经用过,记录的条件是减到最后的值为0

    1. class Solution {
    2. public List<List<Integer>> combinationSum(int[] candidates, int target) {
    3. //记录最终的返回值
    4. List<List<Integer>> res = new LinkedList<>();
    5. //记录当前情况下的路径
    6. Deque<Integer> path = new ArrayDeque<>();
    7. int len = candidates.length;
    8. Arrays.sort(candidates);
    9. dfs(len,target,0,candidates,path,res);
    10. return res;
    11. }
    12. private void dfs(int len, int target, int first, int[] candidates, Deque<Integer> path, List<List<Integer>> res) {
    13. if (target == 0){
    14. res.add(new ArrayList<>(path));
    15. return;
    16. }
    17. for (int i = first; i < len; i++) {
    18. if (target < candidates[i]){
    19. continue;
    20. }
    21. path.add(candidates[i]);
    22. dfs(len,target - candidates[i],i,candidates,path,res);
    23. path.remove(candidates[i]);
    24. }
    25. }
    26. }