组合问题不管是1和2,归根结底还是回溯问题,套上回溯模板就不会错,解决问题的关键在于判断结果集合for循环中的变量。如果是可重复的结果集合,那么在每次循环开始,都应该是上一个值,所以回溯的变量index还是原来的i。而不重复的关键在于去重,去重方法就是先进行排序,排完序后如果下一个数的值和这个值一样,那么就跳过在寻找下一个。
分隔回文串主要解决两个问题,一个是怎么分割,另一个是判断回文。判断回文用双指针可以解决,切割问题是关键。切割可以想象成原来的数字切割,但什么时候算是切割成功呢,那就是当切割后,这一串可以回文,那就切割,否则就前进一位,寻找下一个切割点,直到结束。
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
提示:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
candidates 的所有元素 互不相同
1 <= target <= 40
class Solution {
List<Integer> list = new ArrayList<>();
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
backTracking(target, 0, candidates);
return res;
}
//这个是下面版本的改版,原来每次都要计算一个sum来判断是否满足条件,
//现在只要让target等于0就可以,把每次累加的和进行传递,就不用多浪费
//计算的时间了。
public void backTracking(int target, int index, int[] candidates){
if(target == 0){
res.add(new ArrayList<>(list));
return;
}
for(int i = index; i < candidates.length && candidates[i] <= target; i++){
list.add(candidates[i]);
backTracking(target - candidates[i], i, candidates);
list.remove(list.size() - 1);
}
}
}
//思路比较清晰,就是每次都要重新计算list的和,有点费时间
// class Solution {
// List<Integer> list = new ArrayList<>();
// List<List<Integer>> res = new LinkedList<>();
// public List<List<Integer>> combinationSum(int[] candidates, int target) {
// Arrays.sort(candidates);
// backTracking(target, 0, candidates);
// return res;
// }
// public void backTracking(int target, int index, int[] candidates){
// int sum = 0;
// for(int nums: list){
// sum += nums;
// }
// if(sum == target){
// res.add(new ArrayList<>(list));
// return;
// }
// for(int i = index; i < candidates.length && sum + candidates[i] <= target; i++){
// list.add(candidates[i]);
// backTracking(target, i, candidates);
// list.remove(list.size() - 1);
// }
// }
// }
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
提示:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
class Solution {
List<Integer> list = new ArrayList<>();
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTracking(target, 0, candidates);
return res;
}
public void backTracking(int target, int index, int[] candidates){
if(target == 0){
res.add(new ArrayList<>(list));
return;
}
for(int i = index; i < candidates.length && candidates[i] <= target; i++){
list.add(candidates[i]);
backTracking(target - candidates[i], i + 1, candidates);
list.remove(list.size() - 1);
//去重逻辑,就是最后一个数只要遍历一次,如果下次还是一样的,那就跳过。
while(i + 1 < candidates.length && candidates[i] == candidates[i + 1]){
i++;
}
}
}
}
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
提示:
1 <= s.length <= 16
s 仅由小写英文字母组成
class Solution {
List<String> list = new ArrayList<>();
List<List<String>> res = new LinkedList<>();
public List<List<String>> partition(String s) {
int len = s.length();
char[] arr = new char[len];
for(int i = 0; i < len; i++){
arr[i] = s.charAt(i);
}
backTracking(arr, 0, len);
return res;
}
public void backTracking(char[] arr, int index, int len){
if(index == len){
res.add(new ArrayList<>(list));
return;
}
for(int i = index; i < len; i++){
if(backWord(arr, index, i)){
StringBuilder sb = new StringBuilder();
for(int j = index; j <= i; j++){
sb.append(arr[j]);
}
list.add(sb.toString());
}else{
continue;
}
backTracking(arr, i + 1, len);
list.remove(list.size() - 1);
}
}
public boolean backWord(char[] arr, int begin, int end){
while(begin <= end){
if(arr[begin] != arr[end]){
return false;
}
begin++;
end--;
}
return true;
}
}
分割回文串的思路可以参考卡哥视频,这是我觉得比较好的,重点是切割的方法和终止条件,还有在切割过程中,如果不是回文,要continue继续添加一个字符,这个思路是很重要的。
我们来分析一下切割,其实切割问题类似组合问题。
例如对于字符串abcdef:
组合问题:选取一个a之后,在bcdef中再去选取第二个,选取b之后在cdef中再选取第三个.....。
切割问题:切割一个a之后,在bcdef中再去切割第二段,切割b之后在cdef中再切割第三段.....。
感受出来了不?
所以切割问题,也可以抽象为一棵树形结构,如图:
![](https://i-blog.csdnimg.cn/blog_migrate/02a302358349d2fabb7c265d2fca22a8.jpeg)
递归用来纵向遍历,for循环用来横向遍历,切割线(就是图中的红线)切割到字符串的结尾位置,说明找到了一个切割方法。
此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。
另外分享一个昨天题目中遇到的问题:
力扣216组合总和3问题,可以参考昨天的记录随想录Day25-- 216.组合总和III , 17.电话号码的字母组合_石头走到哪里还是石头的博客-CSDN博客,
//原来的代码没做剪枝,在if条件判断完后没有return语句,代码可以通过
public void backTracking(int n, int k, int index){
if(list.size() == k){
int sum = 0;
for(int nums : list){
sum += nums;
}
if(sum == n){
res.add(new ArrayList<>(list));
}
}
for(int i = index; i <= 9; i++){
list.add(i);
backTracking(n, k, i + 1);
list.remove(list.size() - 1);
}
}
但是一样回溯的函数没有return语句,做了剪枝反而栈溢出了
public void backTracking(int n, int k, int index){
if(list.size() == k){
int sum = 0;
for(int nums : list){
sum += nums;
}
if(sum == n){
res.add(new ArrayList<>(list));
}
}
//这里进行了剪枝
for(int i = index; i <= 9 - (k - list.size()) + 1; i++){
list.add(i);
backTracking(n, k, i + 1);
list.remove(list.size() - 1);
}
}
}
java.lang.StackOverflowError ,这是报错内容。当时一直不明白,为什么一样没有return,没剪枝对,剪枝了反而栈溢出了,当然,在错误代码的第10行那里加一个return就对了,但是为什么没有return就不行呢,后面找到了答案:
这是练友:Java-yui-HIT大三给的关键提醒:那就对了,应该是没加return他就会进循环,比如当k=size的时候,你会发现底下的循环判断条件变成≤10了,然后后面就一直卡递归就爆栈了
然后自己想通了:我明白了,虽然每次递归时,传入的index都会加1,但是list的大小也在加一,所以for循环的退出条件9 - k + list.size()+ 1 的这个条件,就会单调递增,最后两者爆栈,而不剪枝的话,for循环结束条件固定死为i <= 9, 每次循环index都会加1,直到超过9,结束递归,开始退栈,所以可以正常通过。
for循环中其实有两个递归的变量,一个是index,一个是list.size(),之前一直只考虑index的变化情况,忽略了size()的变化,在剪枝或者递归过程,应该要一起考虑这两个变量
上传不了图片,只能复制文字了,也懒得编辑,理解思路就可以。