算法训练营Day27

 39. 组合总和 

39. 组合总和 - 力扣(LeetCode)

注意一下253中,2可以复用的,对应代码,也就是for循环的终止条件是i

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates,target,0,0);
        return result;
    }
    void backTracking(int [] candidates,int target,int sum,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+=candidates[i];
            backTracking(candidates,target,sum,i);//注意这里的i+1的话就是不能重复使用,不加1就是第一个元素可重复使用
            //回溯
            path.removeLast();
            sum-=candidates[i];
        }
    }
}
剪枝操作二刷的时候再看吧!

 40.组合总和II 

40. 组合总和 II - 力扣(LeetCode)

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort( candidates );
        backTracking(candidates,target,0,0);
        return result;
    }
    void backTracking(int [] candidates,int target,int sum,int startIndex){
        if(sum>target){
            return;
        }
        if(sum == target){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i = startIndex;i<candidates.length;i++){
            // 1  1  1 2,第一个1要正常进入递归里面,因为这是路径上的,后面的要continue
            //所以这里是i>startIndex  ==的时候进入递归
            if ( i > startIndex && candidates[i] == candidates[i - 1] ) {
                continue;
            }
            sum+=candidates[i];
            path.add(candidates[i]);
            backTracking(candidates,target,sum,i+1);
            path.removeLast();
            sum-=candidates[i];
        }
    }
}

切割线是startIndex+1,也就是i+1,也就是下一层递归用的新的startIndex  

class Solution {
    List<List<String>> result = new ArrayList<>();
    List<String> path = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backTracking(s,0);
        return result;
    }
    void backTracking(String s,int startIndex){
        if(startIndex==s.length()){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i = startIndex;i<s.length();i++){
            //符合字串,加入path
            if(isPalindrome(s,startIndex,i)){//a,ab  
                //这里开始切,切好了之后在结束条件放进去
                String str = s.substring(startIndex,i+1);
                path.add(str);
            }else{
                continue;
            }
            backTracking(s,i+1);
            path.removeLast();
        }
    }
    private boolean isPalindrome(String s,int i,int j){
        for(;i<j;i++,j--){
            if(s.charAt(i)!=s.charAt(j)){
                return false;
            }
        }
        return true;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值