23.代码随想录算法训练营第二十三天|39. 组合总和,40. 组合总和 II,131. 分割回文串
39. 组合总和 - 力扣(LeetCode)
给你一个 无重复元素 的整数数组 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
思想:
- 第一层for循环的次数由集合中元素的个数决定,表示以该元素开头的组合可以有哪些。
- 递归的层次由目标和决定,当相等的时候就结束递归,大于的时候也结束递归,在这里做了剪枝操作,不可能满足的时候直接跳出循环。
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backtracing(candidates,target,0,0);
return res;
}
public void backtracing(int[] candidates,int target,int sum,int index){
if(sum > target){
return;
}
if(sum == target){
res.add(new ArrayList<>(path));
return;
}
for(int i = index;i < candidates.length;i++){
sum += candidates[i];
path.add(candidates[i]);
backtracing(candidates,target,sum,i);
path.remove(path.size()-1);
sum -= candidates[i];
}
}
}
注意:如果想进行剪枝操作的话,记得先将原有的数组排序,然后在for循环中加一个判断条件,当大于sum的时候,终止循环即可。
40. 组合总和 II - 力扣(LeetCode)
给定一个候选人编号的集合 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
思想:我们要知道,第一层for循环得到的是同一高度节点上的数组的排序。递归函数中得到的是,同一路径上的组合。我们现在的目标是,将数组排序后,已经当过开头的元素就不要再当开头了。
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backtracing(candidates,target,0,0);
return res;
}
public void backtracing(int[] candidates,int target,int sum,int index){
if(sum > target){
return;
}
if(sum == target){
res.add(new ArrayList<>(path));
return;
}
for(int i = index;i < candidates.length;i++){
if(i > index && candidates[i] == candidates [i-1]){
continue;
}
sum += candidates[i];
path.add(candidates[i]);
backtracing(candidates,target,sum,i+1);
path.remove(path.size()-1);
sum -= candidates[i];
}
}
}
注意:如果想要用while循环替代if进行去重操作,需要额外注意下标越界问题,比较麻烦。
131. 分割回文串 - 力扣(LeetCode)
给你一个字符串 s
,请你将 s
分割成一些 子串,使每个子串都是 回文串 。返回 s
所有可能的分割方案。
示例 1:
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a"
输出:[["a"]]
提示:
1 <= s.length <= 16
s
仅由小写英文字母组成
思想:递归用来纵向遍历,for循环用来横向遍历,这里的startindex作为切割的线出现。
class Solution {
List<List<String>> res = new ArrayList<>();
List<String> cur = new ArrayList<>();
public List<List<String>> partition(String s) {
backtracking(s, 0, new StringBuilder());
return res;
}
public void backtracking(String s,int start,StringBuilder sb){
if(start == s.length()){
res.add(new ArrayList(cur));
return;
}
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);
}
}
}
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;
}
}
注意:深入思考,这里的递归终止条件,为什么start == s.length()的时候就能终止了。