代码随想录算法训练营day27|LeetCode 39 组合总和、LeetCode 40 组合总和 II、LeetCode 131 分割回文串

LeetCode 39 组合总和

题目链接:https://leetcode.cn/problems/combination-sum/
视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ/

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

示例1:输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例2:输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例3:输入: candidates = [2], target = 1
输出: []

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
    List<List<Integer>> result = new ArrayList<>();
    Arrays.sort(candidates);// 先进行排序
    backtracking(result, new ArrayList<>(), candidates, target, 0, 0);
    return result;
    }
    public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx){
        if(sum == target){// 找到了数字和为 target 的组合
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = idx; i < candidates.length; i++){
            if(sum + candidates[i] > target) break;// 如果 sum + candidates[i] > target 就终止遍历
            path.add(candidates[i]);
            backtracking(res, path, candidates, target, sum + candidates[i], i);
            path.remove(path.size() - 1);// 回溯,移除路径 path 最后一个元素
        }
    }
}

LeetCode 40 组合总和 II

题目链接:https://leetcode.cn/problems/combination-sum-ii/
视频讲解:https://www.bilibili.com/video/BV12V4y1V73A/

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

示例1:输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例2:输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

class Solution {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> path = new LinkedList<>();
        boolean[] used ;
        int sum = 0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        used = new boolean[candidates.length];// 加标志数组,用来辅助判断同层节点是否已经遍历  
        Arrays.fill(used, false); // 为了将重复的数字都放到一起,所以先进行排序
        Arrays.sort(candidates);
        backtracking(candidates, target, 0);
        return result;
    }
    private void backtracking(int[] candidates, int target, int startIndex){
        if(sum > target) return;
        if(sum == target){
            result.add(new ArrayList<>(path));
        }
        for(int i = startIndex; i < candidates.length; i++){
            if(i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1] ) continue;
            used[i] = true; 
            sum += candidates[i];
            path.add(candidates[i]);
            // 每个节点仅能选择一次,所以从下一位开始
            backtracking(candidates, target, i + 1);
            path.remove(path.size() - 1);
            sum -= candidates[i];
            used[i] = false;
        }
    }
}

LeetCode 131 分割回文串

题目链接:https://leetcode.cn/problems/palindrome-partitioning/
视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6/

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

示例1:输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]

示例2:输入:s = “a”
输出:[[“a”]]

难点:

  • 切割问题可以抽象为组合问题
  • 如何模拟那些切割线
  • 切割问题中递归如何终止
  • 在递归循环中如何截取子串
  • 如何判断回文
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));
        }
        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);// 寻找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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值