Leetcode216/39/40/77之回溯解决经典组合问题

博客 分享
0 266
张三
张三 2022-04-07 11:57:39
悬赏:0 积分 收藏

Leetcode216/39/40/77之回溯解决经典组合问题

Leetcode216-组合总和三

  • 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
  • 只使用数字1到9
  • 每个数字 最多使用一次
  • 返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回
  • 输入: k = 3, n = 7
  • 输出: [[1,2,4]]
  ArrayList<List<Integer>> res=new ArrayList<>();    LinkedList<Integer> integers=new LinkedList<>();    int sum=0;    public List<List<Integer>> combinationSum3(int k, int n) {        combine(k,n,1);        return res;    }    public void combine(int k,int n,int startIndex) {        if (sum != n && integers.size() == k) {            return;        }        if (sum == n && integers.size() == k) {            res.add(new LinkedList<Integer>(integers));        }        for (int i = startIndex; i <= 9; i++) {            sum += i;            integers.addFirst(i);            //使用addFirst和removeFirst每个integers里面是倒序的            //要正序使用add和removeLast就行了            combine(k, n, i + 1);            integers.removeFirst();            sum -= i;        }    }

Leetcode39-组合总和

  • 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
  • candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
  • 输入:candidates = [2,3,6,7], target = 7
  • 输出:[[2,2,3],[7]]
    ArrayList<List<Integer>> res = new ArrayList<>();    LinkedList<Integer> integers = new LinkedList<>();    int sum = 0;    public List<List<Integer>> combinationSum(int[] candidates, int target) {        combine(candidates, target, 0);        return res;    }    public void combine(int[] candidates, int target, int startIndex) {        if (sum > target) {            return;        }        if (sum == target) {            res.add(new LinkedList<>(integers));            return;        }        for (int i = startIndex; i < candidates.length; i++) {            sum += candidates[i];            integers.add(candidates[i]);            combine(candidates, target, i);            integers.removeLast();            sum -= candidates[i];        }    }

Leetcode40-组合总和二

  • 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
  • candidates 中的每个数字在每个组合中只能使用 一次
  • 注意:解集不能包含重复的组合。
  • 输入: candidates = [10,1,2,7,6,1,5], target = 8,
  • 输出:
    [
    [1,1,6],
    [1,2,5],
    [1,7],
    [2,6]
    ]
  ArrayList<List<Integer>> res = new ArrayList<>();    LinkedList<Integer> integers = new LinkedList<>();    int sum = 0;    public List<List<Integer>> combinationSum2(int[] candidates, int target) {        Arrays.sort(candidates);        //加标志数组,用来辅助判断同层节点是否已经遍历        boolean[] flag = new boolean[candidates.length];        combine(candidates, target, 0, flag);        return res;    }    public void combine(int[] candidates, int target, int startIndex, boolean[] flag) {        if (sum > target) {            return;        }        if (sum == target) {            res.add(new LinkedList<>(integers));            return;        }        for (int i = startIndex; i < candidates.length; i++) {            //出现重复节点直接跳过            if (i > 0 && candidates[i] == candidates[i - 1] && flag[i - 1] == false) {                continue;            }            flag[i] = true;            sum += candidates[i];            integers.add(candidates[i]);            combine(candidates, target, i + 1, flag);            integers.removeLast();            sum -= candidates[i];            flag[i] = false;        }    }

L77-组合

  • 给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
  • 你可以按 任何顺序 返回答案
  • 输入:n = 4, k = 2
  • 输出:
    [
    [2,4],
    [3,4],
    [2,3],
    [1,2],
    [1,3],
    [1,4],
    ]
    List<List<Integer>> result = new ArrayList<>();    LinkedList<Integer> path = new LinkedList<>();    public List<List<Integer>> combine(int n, int k) {        combineRes(n,k,1);        return result;    }    public void combineRes(int n,int k,int startIndex){        //终止条件        if(path.size()==k){            result.add(new ArrayList<>(path));//防止递归操作影响以及保存好的path            return;        }        //剪枝优化 此处仔细想想        //如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了。        //已经选择的元素个数:path.size();        //还需要的元素个数为: k - path.size();        //在集合n中至多要从该起始位置 : n - (k - path.size()) + 1,开始遍历        //不剪枝直接 for(int i=startIndex;i<=n;i++)        for(int i=startIndex;i<=n-(k-path.size())+1;i++){            path.addFirst(i);            combineRes(n,k,i+1);            path.removeFirst();        }    }
posted @ 2022-04-07 11:53 fao99 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    张三

    张三 (王者 段位)

    821 积分 (2)粉丝 (41)源码

     

    温馨提示

    亦奇源码

    最新会员