当前位置: 首页 > news >正文

算法笔记|Day20回溯算法II

算法笔记|Day20回溯算法II

  • ☆☆☆☆☆leetcode 39. 组合总和
    • 题目分析
    • 代码
  • ☆☆☆☆☆leetcode 40.组合总和II
    • 题目分析
    • 代码
  • ☆☆☆☆☆leetcode 131.分割回文串
    • 题目分析
    • 代码

☆☆☆☆☆leetcode 39. 组合总和

题目链接:leetcode 39. 组合总和

题目分析

本题采用回溯算法,组合没有数量要求,且元素可无限重复选取,故每次遍历都可以从第一个元素开始。

代码

class Solution {List<List<Integer>> res=new ArrayList<>();List<Integer> path=new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {backtrcking(candidates,target,0,0);return res;}public void backtrcking(int candidates[],int target,int sum,int start){if(sum>target)return;if(sum==target){res.add(new ArrayList(path));return;}for(int i=start;i<candidates.length;i++){sum+=candidates[i];path.add(candidates[i]);backtrcking(candidates,target,sum,i);sum-=candidates[i];path.removeLast();}}
}

☆☆☆☆☆leetcode 40.组合总和II

题目链接:leetcode 40.组合总和II

题目分析

本题集合(数组candidates)有重复元素,但不能有重复的组合,涉及到去重的逻辑,采用了used数组,若该元素在本轮回溯遍历(树层)中用到过赋值为1,后续不再使用,回溯时恢复为0;但在递归遍历(树枝)中用到过,还可以继续使用。

代码

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

☆☆☆☆☆leetcode 131.分割回文串

题目链接:leetcode 131.分割回文串

题目分析

切割问题可以仿照组合问题利用回溯,从前往后搜索,如果发现回文,进入backtracking,起始位置后移一位,循环结束照例移除str的末位。

代码

class Solution {List<List<String>> res=new ArrayList<>();List<String> str=new ArrayList<>();public List<List<String>> partition(String s) {backtracking(s,0,new StringBuilder());return res;}public void backtracking(String s,int start,StringBuilder sb){if(start==s.length()){res.add(new ArrayList(str));return;}for(int i=start;i<s.length();i++){sb.append(s.charAt(i));if(check(sb)){str.add(sb.toString());backtracking(s,i+1,new StringBuilder());str.removeLast();}}}public boolean check(StringBuilder sb){for(int i=0;i<sb.length()/2;i++){if(sb.charAt(i)!=sb.charAt(sb.length()-1-i))return false;}return true;}
}

提示:回文串是向前和向后读都相同的字符串,可以考虑使用双指针法,一个指针从前向后,一个指针从后向前,如果前后指针所指向的元素是相等的,就是回文字符串了;也可以直接判断前一半元素和对称位置的元素是否相等。

http://www.lryc.cn/news/419178.html

相关文章:

  • Oracle认证1Z0-071线上考试注意事项
  • 【C++ 面试 - 基础题】每日 3 题(八)
  • 影响LabVIEW工作效率的因素有哪些
  • linux 裸机.之SPV5210,dnw,usb,sdk,fastboot刷机(一)
  • 性能测试工具LoadRunner
  • 智能归来:深入探索人工智能回归模型的奥秘
  • swift 中,对象() 和 对象.init() 的共同点和异同点
  • Google安装JSON-handle扩展
  • 剖析算法内部结构----------贪心算法
  • uni-app开发微信小程序注意事项,不要用element-ui
  • Hibernate的检索策略(lazy、fetch、batch-size)
  • 算法训练(leetcode)第四十六天 | 110. 字符串接龙、105. 有向图的完全可达性、106. 岛屿的周长
  • 自定义Mybatis-Plus分布式ID生成器(解决ID长度超过JavaScript整数安全范围问题)
  • 2024剪辑神器盘点:四大热门剪辑软件推荐!
  • sql注入靶场sqli-labs常见sql注入漏洞详解
  • [C++] 模板进阶:特化与编译链接全解析
  • oracle-备份
  • oracle 并行parallel的插入insert用法
  • 夜莺监控使用指南
  • MySQLDM笔记-查询库中是否存在列出的表名及查询库中列出的不存在的表名
  • 第9天 xxl-job
  • C++字符串<string>库
  • 智能分析,安全无忧:AI视频分析技术在安全生产中的深度应用
  • 02 Canal的安装使用
  • 【网络安全】玲珑安全第四期
  • 【工具】图片背景移除界面 UI 源码
  • CentOS linux 安装openssl(openssl拒绝服务漏洞【CVE-2022-0778】解决)
  • 假如有一个嵌套集合,怎么通过stream流将集合放到一个集合之中?
  • flutter doctor出现 Unable to find bundled Java version
  • Linux系统修改root密码