代码随想录Day_27打卡

①、组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

事例:

输入:candidates = [2,3,6,7] , 
target = 7

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

思路:

        这道题组合中的数可重复出现,故在递归的时候可递归当前数,即startIndex不用自增,递归结束条件变为当前总和大于等于target,若等于就将组合加入结果集,若大于,则直接结束。

回溯三部曲:

        结束条件:当前总和大于或等于target

       参数及返回值:需要传递数组,当前索引,以及当前总和curSum,当组合每次新增数时,curSum加上新增数,回溯也要相应回溯

        单层逻辑:for循环从startIndex开始,搜索后序组合,可重复,后序递归组合可以从i开始。

代码:

public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates,target,0,0);
        return res;
    }

    private List<List<Integer>> res = new ArrayList<>();
    private LinkedList<Integer> tmp = new LinkedList<>();
    public void backtracking(int[] candidates,int target,int curSum,int startIndex){
        if(curSum > target){
            return;
        }else if(curSum == target){
            res.add(new ArrayList<>(tmp));
            return;
        }

        for(int i = startIndex;i < candidates.length;i++){
            tmp.add(candidates[i]);
            curSum += candidates[i];
            if(curSum > target){
                //直接回溯
                tmp.removeLast();
                curSum -= candidates[i];
                break;
            }
            //递归
            backtracking(candidates,target,curSum,i);
            //回溯
            tmp.removeLast();
            curSum -= candidates[i];
        }
    }

 ②、组合总和Ⅱ

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。 

事例:

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

思路:

        首先将数组排序,将重复元素排到相邻位置,方便去重操作;使用回溯算法,依次得到对应的组合,判断是否符合条件,若符合,加入结果集。

回溯三部曲:

        参数及返回结果:无返回结果,参数为candidates数组,int target,int startIndex,int curSum,int[] used

startIndex 记录当前索引

curSum 记录当前组合中的总和

used 记录数组中对应下标的使用情况 0为未使用 1为使用 即有无在当前组合中

        结束条件:当前组合总和是否大于等于target

        单层逻辑:for循环从startIndex开始,使用递归方法遍历后序组合。

LinkedList<Integer> tmp 当前组合 List<List<Integer>> res 结果集

去重操作:

若当前索引的值和前一个相同,且前一个值没使用,跳过当前值。

因为递归组合中,若两值相同,前一个的组合包括了后一个数的所有组合,而回溯是从for循环依次从小到大开始,若used[i - 1] = 0 则说明前一个数已经组合完 (used[i - 1]已经回溯到0)

如:

代码:

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        int[] used = new int[candidates.length]; //标记当前下标的值是否使用过的数组
        backtracking(candidates,target,0,0,used);
        return res;
    }

    private List<List<Integer>> res = new ArrayList<>();
    private LinkedList<Integer> tmp = new LinkedList<>();

    public void backtracking(int[] candidates,int target,int startIndex,int curSum,int[] used){
        if(curSum == target){
            res.add(new ArrayList<>(tmp));
        }else if(curSum > target){
            return;
        }

        for(int i = startIndex;i < candidates.length;i++){
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == 0){
                //去重
                continue;
            }
            tmp.add(candidates[i]);
            curSum += candidates[i];
            used[i] = 1;
            //递归
            backtracking(candidates,target,i + 1,curSum,used);
            //回溯
            tmp.removeLast();
            curSum -= candidates[i];
            used[i] = 0;
        }
    }

③、分割回文串

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

回文串 是正着读和反着读都一样的字符串。

事例:

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

思路:

        使用回溯算法,在单层循环中,以startIndex为起始索引,i为终止索引,将字符串分割成一个个子字符串,判断该子字符串是否回文,若不回文,则直接跳出当前递归切割,若回文,则加入tmp中继续切割。

代码:

public List<List<String>> partition(String s) {
        backtracking(s,0);
        return res;
    }

    private List<List<String>> res = new ArrayList<>();
    private LinkedList<String> tmp = new LinkedList<>();

    public void backtracking(String s,int startIndex){
        if(startIndex >= s.length()){
            res.add(new ArrayList<>(tmp));
        }

        for(int i = startIndex;i < s.length();i++){
            //判断当前分割线里是否回文
            String stmp = s.substring(startIndex,i + 1);
            if(isHuiwen(stmp)){
                tmp.add(stmp);
            }else continue;
            //递归
            backtracking(s,i + 1);
            //回溯
            tmp.removeLast();
        }
    }

    public boolean isHuiwen(String s){
        int left = 0;
        int right = s.length() - 1;
        while(left < right){
            char c1 = s.charAt(left);
            char c2 = s.charAt(right);
            if(c1 != c2) return false;
            left++;
            right--;
        }
        return true;
    }

参考:代码随想录 (programmercarl.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值