代码随想录算法训练营第二十三天| lc 39. 组合总和 |lc 40. 组合总和 II | lc 131. 分割回文串

题目 组合总和

题目 39. 组合总和
文档链接:代码随想录

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
23 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

思路

回溯算法,遍历范围是index到length,递归深度是sum == target
用时 20min

代码

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        Arrays.sort(candidates);
        back_trace(candidates,res,list,target,0,0);
        return res;
    }
    public void back_trace(int[] candidates,List<List<Integer>> res, List<Integer> list,
                            int target,int index,int sum) {
        //递归终止条件
        if(sum == target) {
            res.add(new LinkedList<>(list));
            return;
        }
        for(int i = index; i < candidates.length; i++) {
            if(sum + candidates[i] > target) continue;
            sum += candidates[i];
            list.add(candidates[i]);
            back_trace(candidates,res,list,target,i,sum);
            sum -= candidates[i];
            list.remove(list.size() - 1);
        }
    }
}

题目 组合总和 II

题目 40. 组合总和 II
文档链接:代码随想录

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。

示例1:

输入: candidates = [10,1,2,7,6,1,5], target = 8
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

思路

回溯算法,循环遍历长度为index到length,递归深度为sum==target,注意重点这道题中每个数字只能使用一次,此时需要先将数组排序,为的是nums[i] == nums[i-1]判断是否是一样的数字,同时使用used[]数组表示是否已经使用过,所以什么条件下表示当前数字可以使用呢?就是上一个数字使用过了当前才可以用(组合问题中相同的数字哪怕不是同一个数字也属于一个答案),即 used[i-1]==true go on,在剪枝代码中就是 !used[i-1] continue
用时 没做出来

代码

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        boolean[] used = new boolean[candidates.length];
        Arrays.sort(candidates);
        back_trace(res,list,target,candidates,0,0,used);
        return res;
    }
    public void back_trace(List<List<Integer>> res, List<Integer> list, int target,
                            int[] candidates, int index, int sum, boolean[] used) {
        //递归终止条件
        if(sum == target) {
            res.add(new LinkedList<>(list));
            return;
        }
        //遍历范围 index -> length
        for(int i = index; i < candidates.length; i++) {
            if(i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) continue;
            used[i] = true;
            list.add(candidates[i]);
            sum += candidates[i];
            back_trace(res,list,target,candidates,i + 1,sum,used);
            used[i] = false;
            sum -= candidates[i];
            list.remove(list.size() - 1);
        }
    }
}

题目 分割回文串

题目 131. 分割回文串
文档链接:代码随想录

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文串
返回 s 所有可能的分割方案。

示例1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

思路

回溯算法,遍历长度是index到length,深度是index == length,题目要切割所有的回文字符串,在单次的递归方程中,循环从index到length,截取的字符串即[index,i],如果该字符串满足回文条件,加入到list中,开始递归回溯
用时 没做出来

代码

class Solution {
    public List<List<String>> partition(String s) {
        List<List<String>> res = new LinkedList<>();
        List<String> list = new LinkedList<>();
        back_trace(s,res,list,0);
        return res;
    }
    public void back_trace(String s, List<List<String>> res, List<String> list, int index) {
        if(index == s.length()) {
            res.add(new LinkedList<>(list));
            return;
        }
        for(int i = index; i < s.length(); i++) {
            if(!huiwen(s,index,i)) continue;
            list.add(s.substring(index,i + 1));
            back_trace(s,res,list,i + 1);
            list.remove(list.size()-1);
        }
    }
    public boolean huiwen(String s, int l, int r) {
        while(l < r) {
            if(s.charAt(l) != s.charAt(r)) return false;
            l++;
            r--;
        }
        return true;
    }
}

看完代码随想录之后的想法&今日收获

回溯题目的模版和代码实现都比较简单,但是要明确在循环中如何找到满足条件的结果比较重要,今天三道题目要熟悉起来,脑海中构建递归树,明确符合条件的结果集

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值