学习目标
- 熟悉回溯相关题目
学习内容
39. 组合总和(Middle)
题目链接:39. 组合总和
题目:给你一个 无重复元素 的整数数组 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
输出: []
思路:模板
时间复杂度:无法估出 空间复杂度:无法估出
解决方案:
class Solution {
List<List<Integer>> ans = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backTracking(candidates, target, 0, 0);
return ans;
}
private void backTracking(int[] candidates, int target, int sum, int start) {
if(sum > target) return;
if(sum == target) {
ans.add(new ArrayList<>(path));
return ;
}
for(int i = start; i < candidates.length; i++) {
sum += candidates[i];
path.add(candidates[i]);
backTracking(candidates, target, sum, i);
path.removeLast();
sum -= candidates[i];
}
}
}
小结:模板
40.组合总和II(Middle)
题目链接:40.组合总和II
题目:给定一个候选人编号的集合 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]
]
思路:模板
解决方案:
class Solution {
List<List<Integer>> ans = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
used = new boolean[candidates.length];
backTracking(candidates, target, 0, 0);
return ans;
}
public void backTracking(int[] candidates, int target, int start, int sum) {
if(sum > target) return ;
if(sum == target) {
ans.add(new ArrayList<>(path));
return ;
}
for(int i = start; i < candidates.length; i++) {
if(i > 0 && candidates[i-1] == candidates[i] && !used[i-1]) continue;
sum += candidates[i];
path.add(candidates[i]);
used[i] = true;
backTracking(candidates, target, i + 1, sum);
used[i] = false;
path.removeLast();
sum -= candidates[i];
}
}
}
小结:这里引入used
作用是确定同一层的相同数字, 如[1,1,2,3,4] targe=6, 必须报告第一个1用了才能用第二个1, 不然会出现两次[1,2,3]
.
131.分割回文串(Middle)
题目链接:131.分割回文串
题目:给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:
输入:s = “a”
输出:[[“a”]]
思路:还是看了 Carl 哥的题解
解决方案:
class Solution {
List<List<String>> ans = new ArrayList<>();
LinkedList<String> path = new LinkedList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0);
return ans;
}
private void backTracking(String s, int start) {
if(start >= s.length()) {
ans.add(new ArrayList(path));
return;
}
for(int i = start; i < s.length(); i++) {
if(isParlindrome(s, start, i)) {
String str = s.substring(start, i+1);
path.add(str);
backTracking(s, i + 1);
path.removeLast();
} else {
continue;
}
}
}
private boolean isParlindrome(String s, int start, int end) {
while(start < end) {
if(s.charAt(start) != s.charAt(end)) return false;
start ++;
end --;
}
return true;
}
}
小结:模板的变化版
总结
回溯要分清楚纵向遍历和横向遍历的对象