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

39. 组合总和

  1. 学习文章链接:
  2. 思路:在组合总和的基础上注意去重逻辑即可。这里也提了个醒,startIndex与 i 之间的关系。
  3. 代码:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if (candidates == null || candidates.length == 0) return res;
        backtracking(candidates, target, 0, 0);
        return res;
    }
    public void backtracking(int[] candidates, int target, int sum, int startIndex) {
        if (sum > target) return;
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            sum += candidates[i];
            path.add(candidates[i]);
            backtracking(candidates, target, sum, i);  
            //将i输入到递归中,这样处理的的去重逻辑很好,只能从当前位置开始遍历,避免了322这种情况的出现。
            sum -= candidates[i];
            path.removeLast();
        }
    }
}

40. 组合总和 II

  1. 学习文章链接:
  2. 思路:
    1. 这题与39.组合总和十分相似,但是不同点在于数组中每个元素只能使用一次,因此递归时需要 i + 1,另外不同点是该题需要跳过同一树层已经使用过的元素,因此使用used数组来记录元素的使用情况。
    2. ( used[i - 1] == true,说明同一树枝candidates[i - 1]使用过 )
    3. ( used[i - 1] == false,说明同一层candidates[i - 1]使用过 )
  3. 代码:
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        if (candidates.length == 0 || candidates == null) return res;
        Arrays.sort(candidates);
        boolean[] used = new boolean[candidates.length];
        backtracking(candidates, target, 0, 0, used);
        return res;
    }
    public void backtracking(int[] candidates, int target, int sum, int startIndex, boolean[] used){
        
        if (sum > target) return;
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
            // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
            // 注意是used[i - 1];
            if (i > 0 && candidates[i - 1] == candidates[i] && used[i - 1] == false) continue;
            sum += candidates[i];
            used[i] = true;
            path.add(candidates[i]);
            backtracking(candidates, target, sum, i + 1, used);
            sum -= candidates[i];
            used[i] = false;
            path.removeLast();
            startIndex++;
        }
    }
}

131.分割回文串

  1. 学习文章链接:
  2. 思路:分割问题与组合问题类似,所以代码的框架类似。详情见如下代码块。
  3. 代码:
class Solution {
    List<List<String>> res = new ArrayList<>();
    LinkedList<String> path = new LinkedList<>();
    public List<List<String>> partition(String s) {
        if (s.length() == 0) return res;
        backtracking(s, 0);
        return res;
    }
    public void backtracking(String s, int index){
        //当index越过最后一个元素时,添加结果到res中
        if (index == s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < s.length(); i++) {
            //判断当前区间内的字符串是否为回文串
            if (isPoliRome(s, index, i)) {
                //截取是回文串的部分,将其添加到path中
                String str = s.substring(index, i + 1);
                path.add(str);
            } else {
                //没有回文串,则跳出当前循环
                continue;
            }
            backtracking(s, i + 1);
            path.removeLast();
        }
    }
    //判断是否为回文串的方法
    public boolean isPoliRome(String str, int start, int end) {
        //双指针法
        for (int i = start, j = end; i < j; i++, j--) {
            if (str.charAt(i) != str.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值