目录

题目一:组合总和

解法一:

解法二:

题目二:组合总和||

解法:

题目三:分割回文串

解法:


题目一:组合总和

力扣题目链接

题目描述:

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合

思路分析:本题和力扣77.组合类似,但区别在于回溯时对startIndex的控制

解法一:

(解法二与之区别不大,可忽略)

class Solution {// 设置全局变量,保存返回结果private List<List<Integer>> res = new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {backtracking(candidates, new LinkedList<>(), 0, target, 0);return res;}// 传入数组,当前路径,当前路径的和,目标数private void backtracking(int[] candidates, List<Integer> path, int pathSum, int target, int startIndex) {if (pathSum == target) {res.add(new LinkedList(path)); // 不new的话,加入的会是同一个pathreturn;}for (int i = startIndex; i < candidates.length; i++) {if (pathSum + candidates[i] <= target) {path.add(candidates[i]);backtracking(candidates, path, pathSum + candidates[i], target, i);path.remove(path.size() - 1);}}}
}

解法二:

class Solution {// 设置全局变量,保存返回结果private List<List<Integer>> res = new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates); // 先进行排序backtracking(candidates, new LinkedList<>(), 0, target, 0);return res;}// 传入数组,当前路径,当前路径的和,目标数private void backtracking(int[] candidates, List<Integer> path, int pathSum, int target, int startIndex) {if (pathSum == target) {res.add(new LinkedList(path)); // 不new的话,加入的会是同一个pathreturn;}for (int i = startIndex; i < candidates.length; i++) {if (pathSum + candidates[i] > target) break;path.add(candidates[i]);backtracking(candidates, path, pathSum + candidates[i], target, i);path.remove(path.size() - 1);}}
}

题目二:组合总和||

力扣题目链接

题目描述:

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

思路分析:(视频指路忍者算法)

与题目一一致,但要注意stratIndex的使用和去重

解法:

class Solution {private List<List<Integer>> res = new LinkedList<>();// 以下两个参数设为全局变量,不用每次在回溯函数传参private int[] candidates;private int target;public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);this.candidates = candidates;this.target = target;backtracking(new LinkedList<>(), 0, 0);return res;}private void backtracking(List<Integer> path, int pathSum, int startIndex) {if (pathSum == target) {res.add(new LinkedList(path));return;}for (int i = startIndex; i < candidates.length; i++) {if (pathSum + candidates[i] <= target) {path.add(candidates[i]);backtracking(path, pathSum + candidates[i], i + 1); // 递归调用,并从集合内下一个元素开始找path.remove(path.size() - 1);}// 在保证不越界的同时,跳过集合内相同的元素while (i != candidates.length - 1 && candidates[i] == candidates[i + 1]) {i++;}} }
}

题目三:分割回文串

力扣题目链接

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

思路分析:同样使用回溯,但要多加一个回文串的判断。建议直接看视频

解法:

class Solution {private List<List<String>> res = new ArrayList<>();public List<List<String>> partition(String s) {backtracking(new ArrayList<>(), s, 0);return res;}private void backtracking(List<String> path, String s, int startIndex) {// 如果起始位置大于s的大小,说明找到了一组分割方案if (startIndex >= s.length()) { // 此时已搜索到叶子节点res.add(new ArrayList(path));return;}for (int i = startIndex; i < s.length(); i++) {if (isPalindrome(s, startIndex, i)) {String str = s.substring(startIndex, i + 1);path.add(str);} else {continue;}backtracking(path, s, i + 1);path.remove(path.size() - 1);}}// 双指针判断是否为回文串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;}
}

代码随想录训练营day27相关推荐

  1. 【代码随想录训练营】Day43-动态规划

    代码随想录训练营 Day43 今日任务 1049.最后一块石头的重量Ⅱ 494.目标和 474.一和零 语言:Java 1049. 最后一块石头的重量Ⅱ 链接:https://leetcode.cn/ ...

  2. 代码随想录训练营day8

    目录 题目一:反转字符串 解法一:数值交换 解法二:位运算 题目二:反转字符串|| 题目三:替换空格 解法一:双指针 解法二:使用额外空间 题目四:翻转字符串里的单词 解法一: 解法二:纯属娱乐 题目 ...

  3. 【代码随想录训练营】【Day14】第六章|二叉树|理论基础|递归遍历|迭代遍历|统一迭代

    理论基础 二叉树的定义形式有:节点指针和数组 在数组中,父节点的下标为i,那么其左孩子的下标即i*2+1,右孩子的下标即为i*2+2 二叉树的常见遍历形式有:前序遍历.后序遍历.中序遍历和层序遍历 前 ...

  4. 代码随想录训练营day56

    题目一:两个字符串的删除操作 力扣题目链接 题目描述: 给定两个单词 word1 和 word2 ,返回使得 word1 和  word2 相同所需的最小步数. 每步 可以删除任意一个字符串中的一个字 ...

  5. 代码随想录训练营day57

    题目一:回文子串 力扣题目链接 题目描述: 给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目. 回文字符串 是正着读和倒过来读一样的字符串. 子字符串 是字符串中的由连续字符组成的一 ...

  6. 代码随想录训练营day53

    题目一:最长公共子序列 力扣题目链接 题目描述: 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . 一个字符串的 子序列  ...

  7. 代码随想录训练营day55

    题目一:判断子序列 力扣题目链接 题目描述: 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符 ...

  8. 代码随想录训练营day52

    题目一:最长上升子序列 力扣题目链接 题目描述: 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度. 子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序. ...

  9. 代码随想录训练营day48

    题目一:打家劫舍 力扣题目链接 题目描述: 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上 ...

最新文章

  1. WinMain中的Console与main中的Window
  2. window.open和window.showModalDialog用法
  3. 二叉树的左右子树交换
  4. POJ2528 线段树+离散化+hash(成段更新)
  5. [leetcode] 912.排序数组
  6. jQuery load()方法特殊用法!
  7. 新年彩蛋:Spring Boot自定义Banner
  8. 双11数据过于完美涉嫌造假?天猫:造谣要负法律责任的哦
  9. 【转】NodeJS教程--基于ExpressJS框架的文件上传
  10. 独立成分分析(Independent Component Analysis)(ICA)(主元分解)
  11. (维基百科LaTeX公式显示异常)解决方法
  12. click事件的执行顺序
  13. GitHub 开发者页面迁至 github.io
  14. java框架面试题及答案,年薪50W
  15. 工作中常见的方法和法则(简单的法则不简单)
  16. excel页码怎么设置从4开始?
  17. oracle imp加快速度,加快IMP速度
  18. C语言fscanf函数的理解
  19. 推荐系统论文笔记(1):Hybrid Recommender Systems:Survey and Experiments
  20. wordpress谷歌字体_适用于WordPress网站的10种性能最高的Google AdSense标语尺寸和格式

热门文章

  1. 单品销量破百万+,登顶天猫类目第一!摇滚动物园的爆品打造攻略你学会了吗?
  2. IBM公司利用人工智能预测化学反应结果
  3. 小型机和服务器有何区别
  4. 学习编程,要不要去IT培训机构?自学和去培训班哪种方法更合适?
  5. Excel 数据透视表教程大全之 04 按日期分组(教程含样本数据)
  6. Python网络爬虫实践(1):爬取网易云音乐播放量大于1000万的歌单
  7. 万字长文浅析:Epoll的那些事儿
  8. 最接近win7的Linux系统,Windows7Vs.Linux——操作系统大PK
  9. 自动生成无课表(云南农业大学)
  10. Echarts给折线图给横竖坐标轴添加箭头与标签文字过长显示不全处理