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

2022-12-11  本文已影响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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读