美文网首页
算法|组合总和、组合总和 II、分割回文串

算法|组合总和、组合总和 II、分割回文串

作者: zhoulikai | 来源:发表于2022-12-11 17:17 被阅读0次

一、 39. 组合总和

题目连接:https://leetcode.cn/problems/combination-sum/

class Solution {
    private List<Integer> paths;
    private List<List<Integer>> result;
    private void backTracking(int[] candidates, int target, int sum, int startIndex) {
    
        //收集结果
        if (sum == target) {
            result.add(new ArrayList<>(paths));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++){
            if (sum + candidates[i] > target) break;
            sum += candidates[i];
            paths.add(candidates[i]);
            backTracking(candidates, target, sum, i);
            sum -= candidates[i];
            paths.remove(paths.size() - 1);
        }
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        paths = new ArrayList<>();
        result = new ArrayList<>();
        Arrays.sort(candidates);
        backTracking(candidates, target, 0, 0);
        return result;
    }
}

二、 40. 组合总和 II

题目连接:https://leetcode.cn/problems/combination-sum-ii/

class Solution {
    private List<Integer> paths;
    private List<List<Integer>> result;
    private void backTracking(int[] candidates, int target, int sum, int indexStart, boolean[] uses) {
        if (sum == target) {
            result.add(new ArrayList<>(paths));
            return;
        }
        for (int i = indexStart; i < candidates.length; i++){
            //剪枝
            if (sum + candidates[i] > target) break;
            //树层去层
            if (i > 0 && candidates[i] == candidates[i - 1] && !uses[i - 1]) continue;
            sum += candidates[i];
            paths.add(candidates[i]);
            uses[i] = true;
            backTracking(candidates, target, sum, i + 1, uses);
            uses[i] = false;
            paths.remove(paths.size() - 1);
            sum -= candidates[i];
        }
    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        paths = new ArrayList<>();
        result = new ArrayList<>();
        Arrays.sort(candidates);
        boolean[] uses = new boolean[candidates.length];
        Arrays.fill(uses, false);
        backTracking(candidates, target, 0, 0, uses);
        return result;
    }
}

三、 131. 分割回文串

题目连接:https://leetcode.cn/problems/palindrome-partitioning/

class Solution {
    List<String> paths;
    List<List<String>> result;
    private boolean isPalind(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;
    }
    private void backTracking(String s, int startIndex){
        if (startIndex >= s.length()) {
            //收集结果
            result.add(new ArrayList<>(paths));
            return;
        }

        for (int i = startIndex; i < s.length(); i++){
            if (isPalind(s, startIndex, i)){
                //是回文添加到paths
                paths.add(s.substring(startIndex, i + 1));
            } else continue;
            backTracking(s, i + 1);
            paths.remove(paths.size() -1);
        }
    }
    public List<List<String>> partition(String s) {
        paths = new ArrayList<>();
        result = new ArrayList<>();
        backTracking(s, 0);
        return result;
    }
}

相关文章

  • 40 组合总和 II

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使...

  • 40、组合总和 II | 算法(leetode,附思维导图 +

    零 标题 & 简介 1 标题 算法(leetode,附思维导图 + 全部解法)300题之(40)组合总和 II 2...

  • 40. 组合总和 II

    40. 组合总和 II(难度:中等) 题目链接:https://leetcode-cn.com/problems/...

  • 组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可...

  • 组合总和

  • 组合总和

    Algorithm 39. Combination Sum[https://leetcode.com/proble...

  • 40. 组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为...

  • 40.组合总和II

    题目给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字...

  • 40.组合总和 II

    自己解法 标准的回溯加剪枝,思路很清晰,但是在细节上出了点小问题,一个是递归的时候应该用i+1,用成了start ...

  • 40. 组合总和 II

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以...

网友评论

      本文标题:算法|组合总和、组合总和 II、分割回文串

      本文链接:https://www.haomeiwen.com/subject/qbmfqdtx.html