代码随想录打卡第二十九天|39. 组合总和 ● 40.组合总和II ● 131.分割回文串

39 组合总和

题目链接39 组合总和
解题思路:这个题目的要求是 某一个数可以无限制的重复被选取,大框架上与216组合总和很相似,只是在递归的时候,表示递归位置的参数index不用加1,这样保证一个元素可以被重复的取
代码如下:

class Solution {
    private List<List<Integer>> result=new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> oneOfresult=new ArrayList<Integer>();
        backtracking(candidates,target,0,0,oneOfresult);
        return result;
    }
    public void backtracking(int[] candidates,int target,int sum,int index,List<Integer> oneOfresult){
        if(sum>target){return;}
        if(sum==target){
            result.add(new ArrayList<Integer>(oneOfresult));
            return;
        }
        for(int i=index;i<candidates.length;i++){
            oneOfresult.add(candidates[i]);
            backtracking(candidates,target,sum+candidates[i],i,oneOfresult);
            oneOfresult.remove(oneOfresult.size()-1);
        }
    }
}

40.组合总和Ⅱ

题目链接:40.组合总和Ⅱ
解题思路:给定的数组中有重复的数字,但题目要求不能有重复的集合,所以此题的重点是去重
本题使用树形去重,要去重的是同一树层上的“使用过”,同一树枝上的都是一个组合里的元素,不用去重
首先要对数组进行排序
下图为树形结构的去重过程,使用used数组来记录是否重复
在这里插入图片描述
在candidates[i] == candidates[i - 1]相同的情况下:

used[i - 1] == true,说明同一树枝candidates[i - 1]使用过 因为是向下递归 所以used[i - 1] == true
used[i - 1] == false,说明同一树层candidates[i - 1]使用过 因为当前candidates[i]是candidates[i - 1]回溯的结果 所以used[i - 1] == false
在这里插入图片描述
另一种去重逻辑 使用index去重

  //跳过同一树层使用过的元素
      if ( i > start && candidates[i] == candidates[i - 1] ) {
        continue;
      }

循环是遍历每一层树 递归是遍历每一支树
所以 在循环过程中 如果遇到同层元素相同时,剪枝 i > start表示从每层第二个开始判断,因为第一个不会重复

class Solution {
    private List<List<Integer>> result=new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> oneOfresult=new ArrayList<Integer>();
        backtracking(candidates,target,0,0,oneOfresult);
        return result;
    }
    public void backtracking(int[] candidates,int target,int sum,int index,List<Integer> oneOfresult){
        if(sum>target){return;}
        if(sum==target){
            result.add(new ArrayList<Integer>(oneOfresult));
            return;
        }
        for(int i=index;i<candidates.length;i++){
        	if ( i > start && candidates[i] == candidates[i - 1] ) {
        		continue;}
            oneOfresult.add(candidates[i]);
            backtracking(candidates,target,sum+candidates[i],i,oneOfresult);
            oneOfresult.remove(oneOfresult.size()-1);
        }
    }
}

131.分割回文串(再看看)

题目链接: 131.分割回文串
解题思路:
本题这涉及到两个关键问题:
1.切割问题,有不同的切割方式
2.判断回文
切割问题的树形结构
在这里插入图片描述
切割问题与组合问题很像 它的index就相当于切割的线一样 所分割的子串是index到i
代码如下:

class Solution {
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return lists;
    }

    private void backTracking(String s, int startIndex) {
        //如果起始位置大于s的大小,说明找到了一组分割方案
        if (startIndex >= s.length()) {
            lists.add(new ArrayList(deque));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            //如果是回文子串,则记录
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                deque.addLast(str);
            } else {
                continue;
            }
            //起始位置后移,保证不重复
            backTracking(s, i + 1);
            deque.removeLast();
        }
    }
    //判断是否是回文串
    private boolean isPalindrome(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没脑袋的喵

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值