代码随想录算法训练营第二十七天 | 39. 组合总和、40.组合总和II、131.分割回文串
39. 组合总和
回溯法
思路:本题抽象为树形结构如下图所示:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates); // 剪枝:先进行排序
backTracking(res, new ArrayList<>(), candidates, target, 0, 0);
return res;
}
public void backTracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int startIndex) {
// 找到了数字和为 target 的组合
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
sum += candidates[i];
// 如果 sum + candidates[i] > target 就终止遍历
if (sum > target) break;
path.add(candidates[i]);
//回溯部分
// 正解 1
// backTracking(res, path, candidates, target, sum + candidates[i], i);
// 正解 2
backTracking(res, path, candidates, target, sum, i);
sum -= candidates[i];
path.remove(path.size() - 1); // 回溯:移除路径中的最后一个元素
}
}
}
40.组合总和II
回溯法
思路:本题涉及去重,即使用过的元素不能重复选取。组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上使用过,一个维度是同一树层上使用过,如下图所示:
本题的“使用过”概念主要针对树层去重,而非树枝去重(原数组须提前排序)。
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> ans = new ArrayList<>();
boolean[] used;
int sum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
used = new boolean[candidates.length];
// 加标志数组,用来辅助判断同层节点是否已经遍历
Arrays.fill(used, false);
// 为了将重复的数字都放到一起,所以先进行排序
Arrays.sort(candidates);
backTracking(candidates, target, 0);
return ans;
}
private void backTracking(int[] candidates, int target, int startIndex) {
// 递归终止条件
if (sum == target) {
ans.add(new ArrayList(path));
}
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]) {
// candidates[i] == candidates[i - 1] 代表排序后的数组有重复元素,
// 是可以利用的,即不需要树枝去重
// !used[i - 1] 代表当前元素的前一位由回溯而来,
// 即同一树层 candidates[i - 1] 使用过,需要跳过
continue;
}
used[i] = true; // 将当前元素标记为使用过
sum += candidates[i];
path.add(candidates[i]);
// 每个节点仅能选择一次,所以从下一位开始
backTracking(candidates, target, i + 1);
// 以下为回溯内容
used[i] = false;
sum -= candidates[i];
path.removeLast();
}
}
}
131.分割回文串
回溯法
思路:本题属于切割问题,但类似组合问题。所以切割问题也可以抽象为一棵树形结构,如图:
本题难点如下:
- 切割问题可以抽象为组合问题
- 如何模拟那些切割线
- 切割问题中递归如何终止
- 在递归循环中如何截取子串
- 如何判断回文
class Solution {
List<List<String>> lists = new ArrayList<>();
Deque<String> deque = new LinkedList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0);
return lists;
}
private void backTracking(String s, int startIndex) {
//如果起始位置大于s的大小,说明找到了一组分割方案,即叶子节点
if (startIndex >= s.length()) {
lists.add(new ArrayList(deque));
return;
}
for (int i = startIndex; i < s.length(); i++) {
//如果是回文子串,则记录
if (isPalindrome(s, startIndex, i)) {
String str = s.substring(startIndex, i + 1);
deque.addLast(str);
} else { // 如果不是则跳过
continue;
}
//起始位置后移,保证不重复
backTracking(s, i + 1);
deque.removeLast();
}
}
//判断是否是回文串(双指针法)
private boolean isPalindrome(String s, int startIndex, int end) {
for (int i = startIndex, j = end; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}