代码随想录算法训练营第二十六天|LeetCode39,40,131

代码随想录算法训练营第二十六天|LeetCode39,40,131

39.组合总数

与上一道组合总数不同的点在于,本题提供的数组中的数值可以重复选取。所以,再次递归的时候,传入的值为i而不是i+1.

本题还需要startIndex来控制for循环的起始位置,对于组合问题,什么时候需要startIndex呢?

如果是一个集合来求组合的话,就需要startIndex,例如:77.组合 ,216.组合总和III
如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:17.电话号码的字母组合

    List<List<Integer>> result;

   List<Integer> path;
   public List<List<Integer>> combinationSum(int[] candidates, int target) {
       result = new ArrayList<>();
       path = new ArrayList<>();
       backtracking(candidates,target,0,0);
       return result;

   }

   public void backtracking(int[]candidates,int target,int startIndex,int sum){
       //剪枝
       if (sum > target){
           return;
       }

       if(sum == target){
           result.add(new ArrayList<>(path));
           return;
       }

       //单层for循环处理:
       for(int i = startIndex; i<candidates.length;i++){
           sum += candidates[i];
           path.add(candidates[i]);

           //关键点,这里传入的index是i,而不是前面几道题的i+1,所以实现了重复选择
           backtracking(candidates, target, i, sum);
           sum-= candidates[i];
           path.remove(path.size()-1);
       }
   }

40.组合总数II

与前几道题相比,本题提供的数组里有重复值,因此,关键点在于去重。
组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上使用过,一个维度是同一树层上使用过。没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因。

那么问题来了,我们是要同一树层上使用过,还是同一树枝上使用过呢?

回看一下题目,元素在同一个组合内是可以重复的,怎么重复都没事,但两个组合不能相同。

所以我们要去重的是同一树层上的“使用过”,同一树枝上的都是一个组合里的元素,不用去重

强调一下,树层去重的话,需要对数组排序!

由于同一树枝上允许重复,而同一树层上需要去重,使用一个used数组来进行区分

  • used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
  • used[i - 1] == false,说明同一树层candidates[i - 1]使用过

used[i-1] == false才能说明,当前判断的这个值是从上一次递归回溯过来的同一级 ,而不是进入到了下一层递归。

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

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

    boolean[] used;


    public void backuptracking(int[] candidates, int target, int sum, int startindex, boolean[] used){
        //做剪枝处理,这个条件可以省略
        if (sum > target){
            return;
        }

        if (sum == target){
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = startindex; i<candidates.length&&candidates[i] + sum <=target;i++){
            // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
            // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
            // 要对同一树层使用过的元素进行跳过
            if (i > 0 && candidates[i] == candidates[i-1] && used[i-1] == false){
                continue;
            }
            sum += candidates[i];
            path.add(candidates[i]);
            used[i] = true;
            backuptracking(candidates,target,sum,i+1,used);
            used[i] = false;
            sum -= candidates[i];
            path.remove(path.size()-1);
        }

    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
        //初始化标志数组,false为当前节点还没有使用过。
        Arrays.fill(used,false);
        //去重,先对数组进行排序
        Arrays.sort(candidates);
        backuptracking(candidates,target,0,0,used);
        return result;

    }

本题也可以不使用used数组来进行去重

private void backTracking( int[] candidates, int target, int start ) {
    if ( sum == target ) {
      res.add( new ArrayList<>( path ) );
      return;
    }
    for ( int i = start; i < candidates.length && sum + candidates[i] <= target; i++ ) {
      //正确剔除重复解的办法
      //跳过同一树层使用过的元素,注意这里是用的i > start而不是i>0,因为for循环是横向移动的,所以i>start,就表明是回溯回来又横向移动了一层。
      if ( i > start && candidates[i] == candidates[i - 1] ) {
        continue;
      }

      sum += candidates[i];
      path.add( candidates[i] );
      // i+1 代表当前组内元素只选取一次
      backTracking( candidates, target, i + 1 );
      sum -=  candidates[i];
      path.removeLast();
    }
  }

131.分隔回文串

通过本题了解到,分隔问题也可以冲向为一颗树形结构,这与组合问题类似。
本题也需要一个startIndex,记录递归开始的位置,而i则为要切割的位置
startIndex为下一次递归开始的位置,当startIndex>=s. length()的时候,就找到了一组分割方案,这个时候就可以终止了。

  List<List<String>> result = new ArrayList<>();

    List<String> path = new ArrayList<>();

    public void backtracking(String s, int startIndex){
        //如果起始位置已经大于s的大小,说明已经找到了一组切割方案
        if(startIndex >= s.length()){
            result.add(new ArrayList<>(path));
            return;
        }

        for(int i = startIndex;i<s.length();i++){
            if (isPalindrome(s,startIndex,i)){
                String str = s.substring(startIndex,i+1);
                path.add(str);
            }else {
                continue;
            }
            backtracking(s,i+1);
            path.remove(path.size()-1);

        }

    }

    //判断是否为回文序列
    boolean isPalindrome(String s, int start, int end){
        for (int  i = start,j = end; i<j;i++,j--){
            if (s.charAt(i) != s.charAt(j)){
                return false;
            }
        }
        return true;
    }

    public List<List<String>> partition(String s) {
        backtracking(s,0);
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值