代码随想录Day22 LeetCode T39 组合总和 T40 组合总和II T131 分割回文串

编程入门 行业动态 更新时间:2024-10-26 03:35:57

代码随想录Day22 LeetCode T39 <a href=https://www.elefans.com/category/jswz/34/1769978.html style=组合总和 T40 组合总和II T131 分割回文串"/>

代码随想录Day22 LeetCode T39 组合总和 T40 组合总和II T131 分割回文串

LeetCode T39 组合总和

题目链接:39. 组合总和 - 力扣(LeetCode)

树形图 

题目思路:

这我们会发现和昨天的题目很像,只是这里的元素并不是只能选取一次了,我们可以根据代码画出树形图来解决问题,下面我们开始递归三部曲

首先我们先定义出result和path数组作为返回值和辅助数组

    List<Integer> path = new LinkedList<>();List<List<Integer>> result = new ArrayList<>();

1.确定回溯函数的参数列表

我们首先肯定要传入candidates数组,因为我们要从中选取元素,传入target,知道我们什么时候要收割正确答案,还要传入一个sum变量来记录我们path中的元素总和,最后是每一层开始可以选取的位置

 public void backtracking(int[] candidates,int target,int num,int startIndex)

2.确定终止条件

这里的终止条件仍然是当target等于sum的时候,我们将收割答案,放到result数组里,当sum比target来的大的时候我们就选择直接return即可

        if(num>target){return;}if(num == target){result.add(new ArrayList(path));return;}

3.确定for循环

这里我们for循环从startindex开始,到candidates数组的大小结束,先向path里面加入元素,sum同步记录,然后进行回溯函数,这里由于candidates数组中的值可以重复取值,这里我们就将下一个取值的起始位置为i,其实也就是这个元素的下标也就是这个取值答案可以向后取的位置,避免重复取值获得[2,3,3] [3,2,3]这样的答案

    for(int i = startIndex;i<candidates.length;i++){path.add(candidates[i]);sum += candidates[i];backtracking(candidates,target,sum,i);path.remove(path.size()-1);sum -= candidates[i];}

题目代码

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

剪枝优化 

其实这道题我们也可以进行剪枝优化的,就是在for循环里面做文章,我们可以先将candidates数组排序,然后如果我们的sum+candidates[i]发现已经大于我们的目标值之后,就可以结束循环了,因为后面不可能再出现我们需要的答案了,这里代码不做过多赘述

LeetCode T40 组合总和II

题目链接: 40. 组合总和 II - 力扣(LeetCode)

tips(我犯的错误) 

错把这个去重当做对candidates数组去重了,实际上第一个1和后面第2个1只是数值相同,并不是可以直接将candidates数组放进set进行去重的,然后使用set对结果集进行去重这种逻辑也很容易超时.

题目思路:

这个题我们引入卡哥的树枝去重和树层去重的概念,树枝去重就是树形图中一次从跟到叶子结点的去重,树层去重就是树的每一层结构中的去重,下面我们先画出树形图

这里我们就发现了树层去重的条件,这里我们以[1,1,2...]举例

这里取第一个1下面的路径已经可以完全包含第二个1的路径,这里我们就可以跳过第二个1对应的循环,这里我们给每个元素对应一个used数组(boolean类型的数组)来定义它的使用情况

我们就会发现满足这样条件的可以直接跳过本轮循环

            if(i>0 && candidates[i] == candidates[i-1] && !used[i-1]){continue;}

回溯三部曲

1.确定参数列表

由于这里的sum和used数组都定义为全局变量了,这里我们就使用candidates数组,target和startIndex作为参数即可

public void backtracking(int[] candidates,int target,int startIndex)

2.确定终止条件

这里和前面一样,不做过多赘述

        if(sum>target){return;}if(sum == target){result.add(new ArrayList(path));}

3.确定for循环

这里进行树层去重

for(int i = startIndex;i<candidates.length;i++){if(i>0 && candidates[i] == candidates[i-1] && !used[i-1]){continue;}used[i] = true;path.add(candidates[i]);sum+=candidates[i];backtracking(candidates,target,i+1);used[i] = false;path.remove(path.size()-1);sum-=candidates[i];}

题目代码:

class Solution {int sum;List<Integer> path = new ArrayList<>();List<List<Integer>> result = new ArrayList<>();boolean used[];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;}public 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;path.add(candidates[i]);sum+=candidates[i];backtracking(candidates,target,i+1);used[i] = false;path.remove(path.size()-1);sum-=candidates[i];}}
}

LeetCode T131 分割回文串

题目链接: 131. 分割回文串 - 力扣(LeetCode)

题目思路:

树形图

这道题有点困难,我们应该模仿组合的思路,一下我列出几个难点,再逐个解决

  • 切割问题可以抽象为组合问题
  • 如何模拟那些切割线 startindex来解决
  • 切割问题中递归如何终止 
  • 在递归循环中如何截取子串
  • 如何判断回文

我们仍然使用回溯三部曲来解决问题,

1.参数列表设计

private void backTracking(String s, int startIndex) {

2.终止条件

这里终止条件就让startindex大于s的长度即可,然后开始收集结果,这里我们判断回文子串的逻辑放在for循环里,从树形结构的图中可以看出:切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止条件。

        if (startIndex >= s.length()) {lists.add(new ArrayList(deque));return;}

3.for循环

这里包括了子串的截取,因为每一层startindex是不变的,而i是一直移动的,这里我们就用[startindex,i]这个闭区间代表每个子串

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();}

4.判断回文

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;}

题目代码:

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) {//如果起始位置大于s的大小,说明找到了一组分割方案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;}
}

更多推荐

代码随想录Day22 LeetCode T39 组合总和 T40 组合总和II T131 分割回文串

本文发布于:2023-12-08 03:17:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1672199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   总和   回文   代码   随想录

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!