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

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

39. 组合总和

class Solution {
    private List<List<Integer>> result = new ArrayList<>();
    private LinkedList<Integer> paths = new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // 看题想到了组合和排列
        // 组合无序,排列有序
        if (candidates.length == 0) {
            return result;
        }
        test(candidates, target, 0, 0);
        return result;
    }
    public void test(int[] candidates, int target, int sum, int startIndex) {
        // 这个是一种剪枝的方法,也可以提前剪枝
        if (sum > target) {
            return;
        }
        if (sum == target) {
            result.add(new ArrayList<>(paths));
            return;
        }
        // 可以在这里剪枝 
        //for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++){
        for (int i = startIndex; i < candidates.length; i++){
            paths.add(candidates[i]);
            sum += candidates[i];
            // 这个是原因,i,不用加一,表示可以重复读取当前的数
            test(candidates, target, sum, i);
            sum -= candidates[i];
            paths.removeLast();
        }
    }
}

40.组合总和II

class Solution {
    public List<List<Integer>> result = new ArrayList<>();
    public LinkedList<Integer> paths = new LinkedList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        test(candidates, target, 0, 0);
        return result;
    }

    public void test(int[] candidates, int target, int sum, int startIndex) {
        if (sum == target) {
            result.add(new ArrayList<>(paths));
            return;
        }
        for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++) {
            // 这里做的去重
            if (i > startIndex && candidates[i] == candidates[i - 1]) {
                continue;
            }

            paths.add(candidates[i]);
            sum += candidates[i];
            test(candidates, target, sum, i + 1);
            sum -= candidates[i];
            paths.removeLast();
        }
    }
}

131.分割回文串

class Solution {
    public List<List<String>> result = new ArrayList<>();
    public LinkedList<String> paths = new LinkedList<>();
    public List<List<String>> partition(String s) {
        test(s, 0);
        return result;
    }

    public void test(String s, int startIndex) {
        if (startIndex >= s.length()) {
            result.add(new ArrayList<>(paths));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            // 我以为往后,其实是往前
            if (ishuiwen(s, startIndex, i)) {
                paths.add(s.substring(startIndex, i+1));
            } else {
                continue;
            }
            test(s, i+1);
            paths.removeLast();
        }
    }

    public boolean ishuiwen(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;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值