力扣刷题笔记27| 39. 组合总和、40.组合总和II、131.分割回文串

文章介绍了LeetCode上的三道题目,分别是组合总和、组合总和II和分割回文串,都是利用回溯算法来找到所有可能的解。在组合总和问题中,元素可无限重复选取;在组合总和II中,元素不可重复选取;分割回文串问题则需要找到字符串的所有回文子串分割方式。每道题目的解法都包括排序数组、回溯遍历以及剪枝操作。
摘要由CSDN通过智能技术生成

39. 组合总和

1.题目:

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

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

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

示例 1:

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

https://leetcode.cn/problems/combination-sum

2.思路:

  • 组合没有数量要求
  • 元素可无限重复选取

3.代码:

public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res=new ArrayList<>();
        List<Integer> path=new ArrayList<>();
        Arrays.sort(candidates);
        backtracking(candidates,target,0,0,path,res);
        return res;
    }
    public void backtracking(int[] candidates,int target,int sum,int idx,List<Integer> path,List<List<Integer>> res){
        if(sum==target){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=idx;i<candidates.length;i++){
            if(sum+candidates[i]>target){
                break;
            }
            sum+=candidates[i];
            path.add(candidates[i]);
            backtracking(candidates,target,sum,i,path,res);
            path.remove(path.size()-1);
            sum-=candidates[i];
        }
    }

4.总结:

40.组合总和II

1.题目:

给定一个候选人编号的集合 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]
]

https://leetcode.cn/problems/combination-sum-ii

2.思路:

  1. 本题candidates 中的每个数字在每个组合中只能使用一次。
  2. 本题数组candidates的元素是有重复的,而39.组合总和 (opens new window)是无重复元素的数组candidates

3.代码:

 public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        //回溯 涉及同一树层剪枝 和 不能取相同的元素
        List<List<Integer>> res=new ArrayList<>();
        List<Integer> path=new ArrayList<>();
        boolean[] used=new boolean[candidates.length];
        Arrays.fill(used,false);
        Arrays.sort(candidates);
        backtaracking(candidates,target,0,0,path,res,used);
        return res;
    }
    public void backtaracking(int[] candidates,int target,int sum,int startindex,List<Integer> path,List<List<Integer>> res,boolean[] used){
        if(sum==target){
            res.add(new ArrayList<>(path));
            return;
        }
        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]);
            backtaracking(candidates,target,sum,i+1,path,res,used);
            path.remove(path.size()-1);
            sum-=candidates[i];
            used[i]=false;
        }
    }

4.总结:

131.分割回文串

1.题目:

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

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

 示例 1:

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

输入:s = "a"
输出:[["a"]]

2.思路:

 分割和组合类似

可拆分成两个子问题:

  1. 切割问题,有不同的切割方式
  2. 判断回文

对于判断回文,比较好写出来

对于分割要明确:
startIndex就是切割线。递归进行分割。注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1

对于for循环:决定了树的宽度,也就是分割线的移动位置。

递归过程是寻找分割出一个字符串后,(分割线向后移动寻找下一个分割线,)扩大宽度的过程。​​​​​​​

3.代码:

class Solution {
    //for循环和递归 for:控制宽度  递归:深度
    List<List<String>> res=new ArrayList<>();
    List<String> path=new ArrayList<>();
    public List<List<String>> partition(String s) {
        backtracking(s,0);
        return res;
    }
    public void backtracking(String s,int startindex){
        if(startindex>=s.length()){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=startindex;i<s.length();i++){
            if(isPalindroom(s,startindex,i)){
                String str=s.substring(startindex,i+1);
                path.add(str);
            }else{
                continue;
            }
            backtracking(s,i+1);
            path.remove(path.size()-1);
        }
    }
    public boolean isPalindroom(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;
    }
}

4.总结:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值