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

39. 组合总和 Combination Sum - LeetCode

可以重复使用元素,没有重复的元素

确定参数和返回值

确定终止条件

单层递归逻辑

二维 res

一维 path//单一结果,收集路径的过程

void backtracking(candidate, target, sum, startIndex)

        if (sum > target) return;

        if (sum == target)

                res.add(path);

                return;

        for (int i = startIndex; i < candidate.length; i++)

                path.add(cadidate[i]);

                sum += candidate[i];

                backtracking(candidate, target, sum, i);

                sum -= candidate[i];

                path.remove(path.size() - 1);

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

    private void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int startIndex) {
        if (sum > target) return;
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex ; i < candidates.length; i++) {
            path.add(candidates[i]);
            sum += candidates[i];
            backtracking(res, path, candidates, target, sum, i);
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
    }
}

40.组合总和II Combination Sum II - LeetCode

树层去重

树枝去重

二维 res

一维 path

Arrays.sort(nums);//一定要记得sort数组

void backtracking(nums, targetSum, sum, startIndex, used)

        if (sum > targetSum) return;

        if (sum == targetSum) 

                res.add(path);

                return;

        for (int i = startIndex; i < nums.length; i++)

                if (i > 0 &&nums[i] == nums[i - 1] && used[i - 1] == 0) continue

                path.add(nums[i]);

                sum += nums[i];

                used[i] = true;

                backtracking(nums, targetSum, sum, i + 1, used);

                path.remove(path.size() - 1);

                sum -= nums[i];

                used[i] = false;

   

class Solution {
    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.sort(candidates);
        backtracking(res, path, candidates, target, used, 0, 0);
        return res;
    }

    private void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, boolean[] used, int sum, int startIndex) {
        if (sum > target) return;
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) continue;
            path.add(candidates[i]);
            sum += candidates[i];
            used[i] = true;
            backtracking(res, path, candidates, target, used, sum, i + 1);
            path.remove(path.size() - 1);
            sum -= candidates[i];
            used[i] = false;
        }
    }
}

      

131.分割回文串Palindrome Partitioning - LeetCode

一维 path//切割方案

二维 res

void backtracking(String s, startIndex)

        if (startIndex >= s.length())

                res.add(path);

                return;

        for (int i = startIndex; i < s.length(); i++)

                isP(s, startIndex + i)

                        path.add(子串)

                else continue

                backtracking(s, i + 1);

                path.remove(子串);

class Solution {
    public List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        List<String> path = new ArrayList<>();
        backtracking(res, path, s, 0);
        return res;
    }

    private void backtracking(List<List<String>> res, List<String> path, String s, int startIndex) {
        if (startIndex >= s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isP(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                path.add(str);
            } else continue;
            backtracking(res, path, s, i + 1);
            path.remove(path.size() - 1);
        }
    }
   private boolean isP(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;
    }

}

                

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值