海智算法训练营第二十七天 | 第七章 回溯算法part03 | ● 39. 组合总和● 40.组合总和II● 131.分割回文串

本文介绍了组合总和问题的两种变体(允许重复和去重),以及如何通过排序和剪枝优化算法。还探讨了分割回文串的问题,涉及递归和回文串检测。学习时长为3小时,重点在于组合问题的深化理解。
摘要由CSDN通过智能技术生成

学习目标:

1.组合总和

2.去重组合总和

3.分割回文串

1.组合总和

组合总和

这道题和组合总和III差不多,区别就在与可以在集合中取相同的数并且没有限制长度,只需要达到目标值就可以,难度就在于剪枝能否可以想到,if(sum + candidates[i] > target)    break;

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<Integer> path = new ArrayList<>();
        dfs(candidates,path,target,0,0);
        return res;
    }

    public void dfs(int[] candidates,List<Integer> path,int target , int start ,int sum){
        if(sum == target){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = start; i < candidates.length; i++) {
            if(sum + candidates[i] > target)    break;
            path.add(candidates[i]);
            dfs(candidates,path,target,i,sum+candidates[i]);
            path.remove(path.size()-1);
        }
    }
}

2.去重组合总和

组合总和II

原理:通过一个判断是否查询过的数组来剪枝

1.先将数组排序,便于去重

2.创建一个used数组记录当前数组下标有没有被使用过

3.if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
                continue;
            }//这时候当前面一个数和后面一个数相等的时候,这时候判断是否是树枝还是树层

(1.树枝:就是以当前数为首位向下延申        2. 树层:首位换了的另一个树枝)

如果是树枝,那么相同的数可以被选择,就比如[1,1,3]这样,但是是树层就不可以,就只能[1,3]。

class Solution {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> ans = new ArrayList<>();
    boolean[] used;
    int sum = 0;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];
        // 加标志数组,用来辅助判断同层节点是否已经遍历
        Arrays.fill(used, false);
        // 为了将重复的数字都放到一起,所以先进行排序
        Arrays.sort(candidates);
        backTracking(candidates, target, 0);
        return ans;
    }

    private void backTracking(int[] candidates, int target, int startIndex) {
        if (sum == target) {
            ans.add(new ArrayList(path));
        }
        for (int i = startIndex; i < candidates.length; i++) {
            if (sum + candidates[i] > target) {
                break;
            }
            // 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过
            if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
                continue;
            }
            used[i] = true;
            sum += candidates[i];
            path.add(candidates[i]);
            // 每个节点仅能选择一次,所以从下一位开始
            backTracking(candidates, target, i + 1);
            used[i] = false;
            sum -= candidates[i];
            path.removeLast();
        }
    }
}

3.分割回文串

题目:分割回文串

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;
    }
}

学习时长:3h

总结:学习了更多关于组合的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值