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

发布时间:2024年01月22日

39. 组合总和

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

解题思路:candidate的元素个数为n,从n的n次方中选出可能的组合,大于目标则抛弃该组合,进行回溯

java:

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates); 
        backtracking(res, new ArrayList<>(), candidates, target, 0, 0);
        return res;
    }
    public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx) {
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = idx; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;
            path.add(candidates[i]);
            backtracking(res, path, candidates, target, sum + candidates[i], i);
            path.remove(path.size() - 1); 
        }
    }
}

40.组合总和II

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

解题思路:先排序,然后遍历,如果当前结点与上一节点相同,则将会出现重复的遍历结果

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates); 
        backtracking(res, new ArrayList<>(), candidates, target, 0, 0);
        return res;
    }
    public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx) {
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = idx; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;
            if(i>idx&&candidates[i]==candidates[i-1]) continue;
            path.add(candidates[i]);
            backtracking(res, path, candidates, target, sum + candidates[i], i+1);
            path.remove(path.size() - 1); 
        }
    }
}

131.分割回文串

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

java:

class Solution {
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();
    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return lists;
    }
    private void backTracking(String s, int startIndex) {
        if (startIndex >= s.length()) {
            lists.add(new ArrayList(deque));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                deque.addLast(str);
            } else {
                continue;
            }
            backTracking(s, i + 1);
            deque.removeLast();
        }
    }
    private boolean isPalindrome(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;
    }
}

文章来源:https://blog.csdn.net/m0_56545264/article/details/135754303
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。