代码随想录训练营第27天| 39. 组合总和、40.组合总和II、131.分割回文串

文章介绍了LeetCode中涉及的三个编程题目,分别是组合总和系列(使用回溯法和动态规划)、组合总和II(基于排序和剪枝)以及分割回文串(使用递归和辅助函数)。这些题目展示了在解决字符串和整数数组问题时的算法技巧。
摘要由CSDN通过智能技术生成

39. 组合总和

题目链接:39. 组合总和 - 力扣(LeetCode)

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtrack(candidates, target, 0, 0, new ArrayList<>());
        return ans;
    }
    private void backtrack(int[] candidates, int target,int index,int sum,List<Integer> list) {
        if(target < sum) {
            return;
        }else if(target == sum) {
            ans.add(new ArrayList<>(list));
            return;
        }else {
            for(int i = index; i < candidates.length; ++i) {
                list.add(candidates[i]);
                sum += candidates[i];
                backtrack(candidates, target, i, sum, list);
                sum -= list.getLast();
                list.remove(list.size()-1);
            } 
        }
    }
}

40.组合总和II

题目链接:40. 组合总和 II - 力扣(LeetCode)

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        int[] used = new int[candidates.length];
        Arrays.sort(candidates);
        backtrack(candidates, target, 0, 0, new ArrayList<>(),used);
        return ans;
    }
    private void backtrack(int[] candidates,int target,int index,int sum,List<Integer> list,int[] used) {
        if(sum > target) {
            return;
        }else if(sum == target) {
            ans.add(new ArrayList<>(list));
        }
        for(int i = index; i < candidates.length; ++i) {
            if(i > 0 && used[i - 1] == 0 && candidates[i] == candidates[i-1]) {
                continue;
            }
            list.add(candidates[i]);
            used[i] = 1;
            sum += candidates[i];
            backtrack(candidates, target, i+1, sum, list, used);
            sum -= list.getLast();
            list.remove(list.size()-1);
            used[i] = 0;
        }
    }
}

131.分割回文串

题目链接:131. 分割回文串 - 力扣(LeetCode)

class Solution {
    List<List<String>> ans = new ArrayList<>();
    List<String> list = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backtrack(s, 0);
        return ans;
    }
    private void backtrack(String s,int index) {
        if(index == s.length()) {
            ans.add(new ArrayList<>(list));
            return;
        }
        for(int i = index; i < s.length(); ++i) {
            if(helper(s.substring(index, i + 1))) {
                list.add(s.substring(index, i + 1));
            }else {
                continue;
            }
            backtrack(s, i + 1);
            list.removeLast();
        }
    }
    private boolean helper(String str) {
        for(int i = 0, j = str.length() - 1; i < j; ++i,--j) {
            if(str.charAt(i) != str.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值