LeetCode题解 18(39,46) 组合总和,全排列<回溯>

组合总和(39)

在这里插入图片描述
注意: 这道题的每同一个元素都可以无限制重复被选取
思路:
回溯算法最常用的框架结构

public void helper(){
 if (满足结束条件)
       // 加入结果集中
        result.add(路径);
        return;
    
    for (选择  选择列表)
        做选择;
        helper(路径, 选择列表);
        回退到上一步;
  }

因此我们先封装一下结果集

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> list = new ArrayList<>();

我们将题目中给的数组进行排序

Arrays.sort(candidates);

这道题我们利用target依次减去数组中的值当减到0的时候就表示已经符合题意就把对应的值封装到Result中。

       if(target == 0){
            res.add(new ArrayList<Integer>(list));
            return;
        }

接着去遍历每一个元素,与target进行减法
需要注意 当某个数组的值大于target时就可以直接退出了 例如 target = 5
candidates[i] = 8, 减后发现小于0。

          for(int i = start;i<candidates.length;i++){
            if(candidates[i] > target){
                break;
            }
          list.add(candidates[i]);
          //递归环节 因为每个元素可以使用多次 因此 递归仍从i开始
          helper(candidates,i,target-candidates[i]);
          //回退到上一层
          list.remove(list.size()-1);
        }

代码解答:

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
          //先排序
          Arrays.sort(candidates);
          helper(candidates,0,target);
          return res;
    }
    public void helper(int[] candidates,int start,int target){
        if(target == 0){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i = start;i<candidates.length;i++){
            if(candidates[i] > target){
                break;
            }
          list.add(candidates[i]);
          helper(candidates,i,target-candidates[i]);
          list.remove(list.size()-1);
        }
    }
}

全排列(46)

在这里插入图片描述
思路:
全排列也就是考虑顺序问题。顺序不同元素一样也是符合的。
我们先定义结果集:

List<List<Integer>> res = new ArrayList<>();

终止的条件就是res的长度与给定数组的长度相同,当满足这一条件就可以返回res

        if(nums.length == list.size()){
            res.add(new ArrayList<Integer>(list));
            return;
        }

因为每个元素只能出现一次,因此当我们查到有相同的元素就continue

        for(int i = 0;i<nums.length;i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(nums,list);
            list.remove(list.size()-1);
        }

代码解答:

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        if(nums == null || nums.length == 0){
            return res;
        }
        helper(nums,new ArrayList<Integer>());
        return res;
    }
    public void helper(int[] nums,ArrayList<Integer> list){
        if(nums.length == list.size()){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i = 0;i<nums.length;i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(nums,list);
            list.remove(list.size()-1);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱敲键盘的程序源

你的激励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值