39. 组合总和
(1)题目描述:

(2)解题思路:
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
if (sum == target) {
result.push_back(path);
return;
}
// 如果 sum + candidates[i] > target 就终止遍历
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
sum += candidates[i];
path.push_back(candidates[i]);
backtracking(candidates, target, sum, i);
sum -= candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
result.clear();
path.clear();
sort(candidates.begin(), candidates.end()); // 需要排序
backtracking(candidates, target, 0, 0);
return result;
}
};
(3)总结:
1.多余的分枝一般体现在for循环上,剪枝时注意for里面多加了条件,所以开始的时候传入的数组要用sort排序
2.树的宽度:整数数组candidates的长度
树的最大深度:当前选择数字组合和为target的数字数量
终止条件:数字组合和为target
剪枝:当和大于target时就return
由于题目中允许一个数可以重复使用,故递归时无需让i+1,仍传i即可
40.组合总和II
(1)题目描述:

[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

[
[1,2,2],
[5]
]
(2)解题思路:
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
if (sum == target) {
result.push_back(path);
return;
}
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 要对同一树层使用过的元素进行跳过
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
continue;
}
sum += candidates[i];
path.push_back(candidates[i]);
used[i] = true;
backtracking(candidates, target, sum, i + 1, used); // 和39.组合总和的区别1,这里是i+1,每个数字在每个组合中只能使用一次
used[i] = false;
sum -= candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<bool> used(candidates.size(), false);
path.clear();
result.clear();
// 首先把给candidates排序,让其相同的元素都挨在一起。
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0, used);
return result;
}
};
(3)总结:
1.注:数层去重如[1,1,2](拿到一个数组也是先排序)第一个1它的树枝上已经包含后面的所有组合情况,所以再碰到和它数值相同的1时,便不用管第二个1,因为都是重复的

2.used数组是来标记那个元素已经用过,用过的就标记为true,如[0,1,0],我们便知道使用了数组中的第二个元素
131.分割回文串
(1)题目描述:

(2)解题思路:
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
if (sum == target) {
result.push_back(path);
return;
}
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 要对同一树层使用过的元素进行跳过
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
continue;
}
sum += candidates[i];
path.push_back(candidates[i]);
used[i] = true;
backtracking(candidates, target, sum, i + 1, used); // 和39.组合总和的区别1,这里是i+1,每个数字在每个组合中只能使用一次
used[i] = false;
sum -= candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<bool> used(candidates.size(), false);
path.clear();
result.clear();
// 首先把给candidates排序,让其相同的元素都挨在一起。
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0, used);
return result;
}
};
(3)总结:
1.回文串是指一个字符串正读反读都一样
2.终止条件就是切割线已经在这个字符串的最后了,这里startIndex其实就是切割线

3.substr 用于从一个字符串中提取从指定位置开始的指定长度的子字符串。string.substr(start, length);
• start:子字符串的起始位置(从 0 开始计数)
• length:要提取的子字符串的长度。如果省略,则提取从 start 开始到字符串末尾的所有字符