day22|39. 组合总和、40.组合总和II、131.分割回文串

给你一个 无重复元素 的整数数组 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 。 仅有这两种组合。  

示例 2:

输入: candidates = [2,3,5], target = 8

输出: [[2,2,2,2],[2,3,3],[3,5]] 

示例 3:

输入: candidates = [2], target = 1

输出: []

 问题分析:

1、递归函数返回值和参数

数组candidates、目标和target、记录同一数组搜索开始的下标位startIndex

2、终止条件

sum>target收尾

sum==target,把路径收集到result二维集合中

3、单层搜索的过程

for循环依然从startIndex开始,只不过本题可以重复选择已选过的元素,即回溯时为i而不是i+1,下一层还可以选已选过的元素。例如选2,下一层可以选2367;选3,下一层可以选367.

class Solution {
        List<Integer> path=new ArrayList<>();
        List<List<Integer>> result=new ArrayList<>();
        int sum=0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0);
        return result;
    }

    public void backtracking(int[] candidates,int target,int startIndex){
        if (sum>target) return;
        if (sum==target){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i=startIndex;i<candidates.length;i++){
            path.add(candidates[i]);
            sum=sum+candidates[i];
            backtracking(candidates,target,i);//不能用index,也不是i+1
            sum=sum-candidates[i];
            path.remove(path.size()-1);
        }
    }
}

优化:

剪枝 :当sum+candidates[i]>target就跳出循环

class Solution {
        List<Integer> path=new ArrayList<>();
        List<List<Integer>> result=new ArrayList<>();
        int sum=0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);//要排序,从小到大,否则在下面的for循环中,
                                    // 当出现比target大的数就会跳出循环,返回空
        backtracking(candidates,target,0);
        return result;
    }

    public void backtracking(int[] candidates,int target,int index){
        if (sum>target) return;
        if (sum==target){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i=index;i<candidates.length&&sum+candidates[i]<=target;i++){//剪枝
            path.add(candidates[i]);
            sum=sum+candidates[i];
            backtracking(candidates,target,i);//不能用index
            sum=sum-candidates[i];
            path.remove(path.size()-1);
        }
    }
}

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] ] 

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,

输出: [ [1,2,2], [5] ] 

问题分析: 

1、递归函数返回值和参数

数组candidates、目标和target、记录同一数组搜索开始的下标位startIndex

2、终止条件

sum>target收尾

sum==target,把路径收集到result二维集合中

3、单层搜索的过程

for循环依然从startIndex开始,因为解集不能包含重复的组合,所以先排序,

例如1,1,2,5,6,7,10,当第二个数1与前面的数重复时,要跳出循环,遍历下一个数,也就是去重。

class Solution {
        List<Integer> path=new ArrayList<>();
        List<List<Integer>> result=new ArrayList<>();
        int sum=0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(candidates,target,0);
        return result;
    }
    public void backtracking(int[] candidates,int target,int startIndex){
        if (sum>target) return;
        if (sum==target){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i=startIndex;i<candidates.length&&sum+candidates[i]<=target;i++){//剪枝
            if (i>startIndex&&candidates[i]==candidates[i-1]){//去重,不能包含重复元素
                continue;
            }
            path.add(candidates[i]);
            sum=sum+candidates[i];
            backtracking(candidates,target,i+1);
            sum=sum-candidates[i];
            path.remove(path.size()-1);
        }
    }
}

131.分割回文串

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

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

示例 1:

输入:s = "aab"

输出:[["a","a","b"],["aa","b"]]   

示例 2:

输入:s = "a"

输出:[["a"]] 

问题分析:

1、递归函数参数

全局变量数组path存放切割后回文的子串,二维数组result存放结果集。 

本题递归函数参数还需要startIndex,因为切割过的地方,不能重复切割。

2、递归函数终止条件

切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止条件。

递归参数需要传入startIndex,表示下一轮递归遍历的起始位置,startIndex就是切割线

3、单层搜索的逻辑

for (int i = startIndex; i < s.size(); i++)循环中,定义了起始位置startIndex,那么 [startIndex, i] 就是要截取的子串。

首先判断这个子串是不是回文,如果是回文,就加入path中,path用来记录切割过的回文子串。

因为左闭右闭,所以backtracking(s, i + 1); 传入下一层的起始位置为i + 1。

 

class Solution {
        List<String> path=new ArrayList<>();
        List<List<String>> result=new ArrayList<>();

    public List<List<String>> partition(String s) {
        backtracking(s,0);
        return result;
    }
    public void backtracking(String s,int startIndex){//startIndex就是切割线
        if (startIndex>=s.length()){//左闭右闭
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i=startIndex;i<s.length();i++){//判断回文
            if (isPalindrome(s,startIndex,i)){//如果是回文子串
                String str=s.substring(startIndex,i+1);//substring只含开头不含结尾
                path.add(str);
            }
            else continue;//如果不是直接跳到下一个i
            backtracking(s,i+1);
            path.remove(path.size()-1);

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值