java-代码随想录第23天|39. 组合总和、40.组合总和II、131.分割回文串
目录
39. 组合总和
40.组合总和II
131.分割回文串
学习链接:代码随想录
39. 组合总和
题目:
给你一个 无重复元素 的整数数组
candidates
和一个目标整数target
,找出candidates
中可以使数字和为目标数target
的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates
中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。对于给定的输入,保证和为
target
的不同组合数少于150
个。
回溯三部曲
1.递归函数参数
定义两个全局变量,二维数组result存放结果集,数组path存放符合条件的结果。(这两个变量可以作为函数参数传入)
2.递归终止条件
sum大于target和sum等于target。
sum等于target的时候,需要收集结果。
3.单层搜索的逻辑
单层for循环依然是从startIndex开始,搜索candidates集合。
// 剪枝优化
class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(candidates); // 先进行排序backtracking(res, new ArrayList<>(), candidates, target, 0, 0);return res;}public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx) {// 找到了数字和为 target 的组合if (sum == target) {res.add(new ArrayList<>(path));return;}for (int i = idx; i < candidates.length; i++) {// 如果 sum + candidates[i] > target 就终止遍历if (sum + candidates[i] > target) break;path.add(candidates[i]);backtracking(res, path, candidates, target, sum + candidates[i], i);path.remove(path.size() - 1); // 回溯,移除路径 path 最后一个元素}}
}
40.组合总和II
题目:
给定一个候选人编号的集合
candidates
和一个目标数target
,找出candidates
中所有可以使数字和为target
的组合。
candidates
中的每个数字在每个组合中只能使用 一次 。注意:解集不能包含重复的组合。
数组candidates有重复元素,而要求不能有重复的组合,所以相对于39.组合总和 (opens new window)难度提升了不少。
使用树层去重,树枝不去重。
回溯三部曲:
1.递归函数参数
定义两个全局变量,二维数组result存放结果集,数组path存放符合条件的结果。(这两个变量可以作为函数参数传入)
还需要加一个bool型数组used,用来记录同一树枝上的元素是否使用过。
2.递归终止条件
终止条件为 sum > target
和 sum == target。
3.单层搜索的逻辑
如果candidates[i] == candidates[i - 1]
并且 used[i - 1] == false
,就说明:前一个树枝,使用了candidates[i - 1],也就是说同一树层使用过candidates[i - 1]。
此时for循环里就应该做continue的操作。
class Solution {List<List<Integer>> res = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();int sum = 0;public List<List<Integer>> combinationSum2( int[] candidates, int target ) {//为了将重复的数字都放到一起,所以先进行排序Arrays.sort( candidates );backTracking( candidates, target, 0 );return res;}private void backTracking( int[] candidates, int target, int start ) {if ( sum == target ) {res.add( new ArrayList<>( path ) );return;}for ( int i = start; i < candidates.length && sum + candidates[i] <= target; i++ ) {//正确剔除重复解的办法//跳过同一树层使用过的元素if ( i > start && candidates[i] == candidates[i - 1] ) {continue;}sum += candidates[i];path.add( candidates[i] );// i+1 代表当前组内元素只选取一次backTracking( candidates, target, i + 1 );int temp = path.getLast();sum -= temp;path.removeLast();}}
}
131.分割回文串
题目:给你一个字符串
s
,请你将s
分割成一些 子串,使每个子串都是 回文串 。返回s
所有可能的分割方案。
注意:
1.如何表示切割线
2.如何表示切割范围
class Solution {//保持前几题一贯的格式, initializationList<List<String>> res = new ArrayList<>();List<String> cur = new ArrayList<>();public List<List<String>> partition(String s) {backtracking(s, 0, new StringBuilder());return res;}private void backtracking(String s, int start, StringBuilder sb){//因为是起始位置一个一个加的,所以结束时start一定等于s.length,//因为进入backtracking时一定末尾也是回文,所以cur是满足条件的if (start == s.length()){//注意创建一个新的copyres.add(new ArrayList<>(cur));return;}//像前两题一样从前往后搜索,如果发现回文,//进入backtracking,起始位置后移一位,循环结束照例移除cur的末位for (int i = start; i < s.length(); i++){sb.append(s.charAt(i));if (check(sb)){cur.add(sb.toString());backtracking(s, i + 1, new StringBuilder());cur.remove(cur.size() -1 );}}}//helper method, 检查是否是回文private 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;}
}