代码训练营day27-组合和分割

组合总和和回文数的分割

1. 组合总和(leetcode39)

题目:给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,
找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。 你可以按 任意顺序 返回这些组合。 candidates 中的 同一个 数字可以 无限制重复被选取 。
如果至少一个数字的被选数量不同,则两种组合是不同的。 对于给定的输入,保证和为 target 的不同组合数少于 150 个

总结:不需要去重,数组中无重复的元素,但是要求同一个数字可重复使用,这时index在递归时不需要加1


class Solution {
       List<List<Integer>> result;
       LinkedList<Integer> path;

       public List<List<Integer>> combinationSum(int[] candidates, int target) {
           result = new ArrayList<>();
           path = new LinkedList<>();
           travelbacking(candidates, target, 0, 0);
           return result;

       }

       // 返回值用void, 不需要统计调用次数
       public void travelbacking(int[] candidates, int target, int sum, int index) {
           // 剪枝
           if (sum > target) {
               return;
           }

           if (sum == target) {
               result.add(new ArrayList<>(path));
               // sum = 0; // 这里sum不用归零
               return;
           }

           for (int i = index; i < candidates.length; i++) { // 这里不能写i=0,应该写index,保证元素可重复
               path.add(candidates[i]);
               sum += candidates[i];
               travelbacking(candidates, target, sum, i); // todo 这一步很关键,并不是i+1,因为可重复
               path.removeLast();
               sum -= candidates[i]; // 一定要进行回溯
           }
       }
   }

2. 组合总和2(leetcode40)

题目:给定一个候选人编号的集合 candidates 和一个目标数 target ,
找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次
注意:解集不能包含重复的组合。

总结:要去重,排序减少复杂度,此外树层不能重复

    class Solution {
        List<List<Integer>> result;
        LinkedList<Integer> path;
        boolean[] used;

        public List<List<Integer>> combinationSum2(int[] candidates, int target) {
            used = new boolean[candidates.length];
            Arrays.fill(used, false);
            Arrays.sort(candidates); // 排序是避免结果重复的关键
            result = new ArrayList<>();
            path = new LinkedList<>();
            travelbacking(candidates, target, 0, 0);
            return result;
        }

        // 返回值用void, 不需要统计调用次数
        public void travelbacking(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++) {
                // 确保同一层不使用重复元素
                if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
                    continue;
                }
                path.add(candidates[i]);
                sum += candidates[i];
                used[i] = true;
                travelbacking(candidates, target, sum, i + 1); // 正确使用i + 1避免重复
                path.removeLast();
                used[i] = false;
                sum -= candidates[i]; // 回溯
            }
        }
    }

3. 回文数的分割(leetcode131)

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

index作为切割线,回文数如何求

    class Solution {

        List<String> path;
        List<List<String>> result;

        public List<List<String>> partition(String s) {
            path = new LinkedList<>();
            result = new ArrayList<>();
            travelBacking(s, 0);
            return result;
        }

        private void travelBacking(String s, int index) {
            // 当index等于字符串长度时,表示已经遍历完字符串,此时的path就是一种分割方案
            if (index == s.length()) {
                result.add(new ArrayList<>(path));
                return;
            }

            for (int i = index; i < s.length(); i++) {
                // 检查从index到i的子串是否为回文串
                if (isPalindrome(s, index, i)) {
                    // 是回文串则加入path,并继续对剩余子串进行分割
                    path.add(s.substring(index, i + 1));
                    travelBacking(s, i + 1);
                    // 回溯,移除刚刚加入的子串
                    path.remove(path.size() - 1);
                }
            }
        }

        // 判断是否是回文串
        private boolean isPalindrome(String s, int startIndex, int endIndex) {
            while (startIndex < endIndex) {
                if (s.charAt(startIndex) != s.charAt(endIndex)) {
                    return false;
                }
                startIndex++;
                endIndex--;
            }
            return true;
        }
    }
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值