39.组合总和
题目链接/文章讲解: 代码随想录
第一想法:
组合总和与第22天组合总和III的区别在于元素可以重复,其实仅在于 startIndex上的控制,从图中可以看出startIndex不再+1。
所以startIndex +1 作用:是确保结果集不可以重复取值。
并且可以看到:
sum == target ;加入结果集,返回
sum >target :不加入结果集,直接返回
sum<target :继续递归
所以这三个条件应写在同一层级 if -else语句中,即对结果的判断,而不是这种剪枝代码:
for (int i = startIndex; i <= 9-(k-path.size())+1; i++) {
sum+=i;
path.add(i);
if(sum>n){
sum-=i;
path.remove(path.size()-1);
return;
}
getPath(n,k,i+1);
sum-=i;
path.remove(path.size()-1);
}
示意图:
代码
class Solution {
List<Integer> path = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
getPath(candidates,target,0);
return result;
}
public void getPath(int[] candidates,int target,int startIndex){
if(sum==target){
result.add(new ArrayList<>(path));
return;
} else if (sum > target){
return;
}
else{
for(int i = startIndex;i<candidates.length;i++){
path.add(candidates[i]);
sum+=candidates[i];
getPath(candidates,target,i);
sum-=candidates[i];
path.remove(path.size()-1);
}
}
}
}
40.组合总和II
题目链接/文章讲解:代码随想录
视频讲解:回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili
第一想法:
输入: candidates = [2,5,2,1,2], target = 5, 这个示例画图后会发现得到[2,2,1]和[ 2,1,2]两个结果,判定为同一结果,应当去重。结果集用set去重
代码随想录:
关于去重的理解:
这里卡哥讲的真的很深入。搜索的过程中去掉重复组合。所谓去重,就是使用过的元素不能重复选取。
组合问题可以抽象为树形结构,那么使用过在这个树形结构上具有两个维度。即树层和数值。
同一树枝上都是一个组合里的元素,不用去重。
树层去重需要排序,要使两个相同元素紧挨在一起方便去重。
不可以重复选取1是因为:第一个1已经包含了所有以1能满足结果的情况,如果再以第二个1开始构造结果,势必会造成重复。
假设数组[1,1,2,3,5,6] target = 8,以第一个1构造可以得到[1,1,6],[1,2,5]....以第二个1构造会得到[1,2,5],这就会造成重复。
如何横向去重:
那么如何横向去重呢:数组需要排序,i>startIndex && digits[i]==digits[i-1] continue;
我提一下我个人的理解,假如在for循环中的if语句不用卡哥所说的i >0而是用i>index,这样就可以不用uesd数组了。因为如果i>index&&candidates【i】==candidates【i-1】,这样可以保证在每次backtracking中相邻元素相同值的 for循环的第二次被去重,而再次使用backtracking的时候带入的是新的index+1参数,这样就可以在不使用uesd数组的情况下,避免出现【1,1,1,2】要和为3时子集【1,1,1】被忽略的问题。
代码:
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int sum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
getPath(candidates,target,0);
return result;
}
public void getPath (int[] candidates,int target,int startIndex){
if(sum==target){
result.add(new ArrayList<>(path));
return;
}
else if(sum>target){
return;
}else{
for(int i = startIndex;i<candidates.length;i++){
if(i>startIndex&&candidates[i]==candidates[i-1])
continue;
path.add(candidates[i]);
sum+=candidates[i];
getPath(candidates,target,i+1);
sum-=candidates[i];
path.remove(path.size()-1);
}
}
}
}
131.分割回文串
代码随想录
需要自己一边写代码一遍画图才能体会到。
代码
class Solution {
List<List<String>> result = new ArrayList<>();
List<String> itemPath = new ArrayList<>();
public List<List<String>> partition(String s) {
getPath(s,0);
return result;
}
public void getPath(String s,int startIndex){
if(startIndex>=s.length()){
result.add(new ArrayList<>(itemPath));
return;
}
for(int i = startIndex;i<s.length();i++){
//截取一个字符串 subString 左闭右开,"aab".subString(0,3) = "aab";
String subString = s.substring(startIndex,i+1);
if(isPalindrome(subString)){
itemPath.add(subString);//如果是回文串,则加入
}else{
continue;
}
getPath(s,i+1);
itemPath.remove(itemPath.size()-1);
}
}
public boolean isPalindrome(String s){
for(int i = 0,j=s.length()-1;i<j;i++,j--){
if(s.charAt(i)!=s.charAt(j)){
return false;
}
}
return true;
}
}