39. 组合总和
1.题目:
给你一个 无重复元素 的整数数组 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 。
仅有这两种组合。
https://leetcode.cn/problems/combination-sum
2.思路:
- 组合没有数量要求
- 元素可无限重复选取
3.代码:
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
List<Integer> path=new ArrayList<>();
Arrays.sort(candidates);
backtracking(candidates,target,0,0,path,res);
return res;
}
public void backtracking(int[] candidates,int target,int sum,int idx,List<Integer> path,List<List<Integer>> res){
if(sum==target){
res.add(new ArrayList<>(path));
return;
}
for(int i=idx;i<candidates.length;i++){
if(sum+candidates[i]>target){
break;
}
sum+=candidates[i];
path.add(candidates[i]);
backtracking(candidates,target,sum,i,path,res);
path.remove(path.size()-1);
sum-=candidates[i];
}
}
4.总结:
40.组合总和II
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]
]
https://leetcode.cn/problems/combination-sum-ii
2.思路:
- 本题candidates 中的每个数字在每个组合中只能使用一次。
- 本题数组candidates的元素是有重复的,而39.组合总和 (opens new window)是无重复元素的数组candidates
3.代码:
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
//回溯 涉及同一树层剪枝 和 不能取相同的元素
List<List<Integer>> res=new ArrayList<>();
List<Integer> path=new ArrayList<>();
boolean[] used=new boolean[candidates.length];
Arrays.fill(used,false);
Arrays.sort(candidates);
backtaracking(candidates,target,0,0,path,res,used);
return res;
}
public void backtaracking(int[] candidates,int target,int sum,int startindex,List<Integer> path,List<List<Integer>> res,boolean[] used){
if(sum==target){
res.add(new ArrayList<>(path));
return;
}
for(int i=startindex;i<candidates.length;i++){
if(sum+candidates[i]>target){
break;
}
if(i>0 && candidates[i]==candidates[i-1] && !used[i-1]){
continue;
}
used[i]=true;
sum+=candidates[i];
path.add(candidates[i]);
backtaracking(candidates,target,sum,i+1,path,res,used);
path.remove(path.size()-1);
sum-=candidates[i];
used[i]=false;
}
}
4.总结:
131.分割回文串
1.题目:
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
2.思路:
分割和组合类似
可拆分成两个子问题:
- 切割问题,有不同的切割方式
- 判断回文
对于判断回文,比较好写出来
对于分割要明确:
startIndex就是切割线。递归进行分割。注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1
对于for循环:决定了树的宽度,也就是分割线的移动位置。
递归过程是寻找分割出一个字符串后,(分割线向后移动寻找下一个分割线,)扩大宽度的过程。
3.代码:
class Solution {
//for循环和递归 for:控制宽度 递归:深度
List<List<String>> res=new ArrayList<>();
List<String> path=new ArrayList<>();
public List<List<String>> partition(String s) {
backtracking(s,0);
return res;
}
public void backtracking(String s,int startindex){
if(startindex>=s.length()){
res.add(new ArrayList<>(path));
return;
}
for(int i=startindex;i<s.length();i++){
if(isPalindroom(s,startindex,i)){
String str=s.substring(startindex,i+1);
path.add(str);
}else{
continue;
}
backtracking(s,i+1);
path.remove(path.size()-1);
}
}
public boolean isPalindroom(String s,int start,int end){
for(int i=start,j=end;i<j;i++,j--){
if(s.charAt(i)!=s.charAt(j)){
return false;
}
}
return true;
}
}
4.总结: