继续回溯。今天第一题是组合总数https://leetcode.cn/problems/combination-sum/description/,直接用回溯算法的代码套路写出。由于重复元素可以选取,在递归时不必从当前元素的下一个进行递归。
class Solution {
public:
vector> result;
vector path;
void backtracking(int targetsum, int sum, vector& candidates,
int startindex) {
if (sum > targetsum)
return;
if (sum == targetsum) {
result.push_back(path);
return;
} //终止条件
for (int i = startindex; i < candidates.size(); i++) {
path.push_back(candidates[i]);
sum += candidates[i];
backtracking(targetsum, sum, candidates, i); //递归
sum -= candidates[i];
path.pop_back();
}
}
vector> combinationSum(vector& candidates, int target) {
backtracking(target, 0, candidates, 0);
return result;
}
};
该题的剪枝操作需要先将数组排序,发现当前层的sum与遍历元素和大于targetsum时,对于后序更大的元素就没必要遍历了。
class Solution {
public:
vector> result;
vector path;
void backtracking(int targetsum, int sum, vector& candidates,
int startindex) {
if (sum > targetsum)
return;
if (sum == targetsum) {
result.push_back(path);
return;
} //终止条件
for (int i = startindex;
i < candidates.size() && sum + candidates[i] <= targetsum; i++) {//剪枝
path.push_back(candidates[i]);
sum += candidates[i];
backtracking(targetsum, sum, candidates, i); //递归
sum -= candidates[i];
path.pop_back();
}
}
vector> combinationSum(vector& candidates, int target) {
sort(candidates.begin(), candidates.end());//排序
backtracking(target, 0, candidates, 0);
return result;
}
};
第二题是组合总数IIhttps://leetcode.cn/problems/combination-sum-ii/,第一遍写出的代码没有将最终的result数组去重,编译不通过。第二遍写出的代码虽然进行了去重,但仍没有通过,看了卡哥的视频才发现是去重去多了,错误进行了树枝去重而不是树层去重。used去重很巧妙。
class Solution {
public:
vector> result;
vector path;
void backtracking(int target, int sum, int startindex, vector& candidates, vector& used){
if (sum > target) return;
if (sum == target){
result.push_back(path);
return;
}
for(int i = startindex; i < candidates.size(); i++){
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;
path.push_back(candidates[i]);
sum += candidates[i];
used[i] = true;
backtracking(target, sum, i + 1, candidates,used);
path.pop_back();
sum -= candidates[i];
used[i] = false;
}
}
vector> combinationSum2(vector& candidates, int target) {
vector used (candidates.size(), false);
sort(candidates.begin(), candidates.end());
backtracking(target, 0, 0, candidates, used);
return result;
}
};
第三题是分割回文串https://leetcode.cn/problems/palindrome-partitioning/description/,这题确实第一次做没思路。看了卡哥讲解发现确实是一道难题,切割过程用组合方式模拟不好想,细节很多。代码还是与模板很契合的。
class Solution {
public:
vector> result;
vector path;
bool is(string& s, int startindex, int end){//判断回文串
for (int i = startindex, j = end; i < j; i++, j--){
if (s[i] != s[j]) return false;
}
return true;
}
void backtracking(string& s, int startindex){
if (startindex >= s.size()){//终止条件即切割线位于串末尾
result.push_back(path);
return;
}
for(int i = startindex; i < s.size(); i++){
if (is(s, startindex, i)){
string str = s.substr(startindex, i - startindex + 1);
path.push_back(str);
}
else continue;
backtracking(s, i + 1);
path.pop_back();
}
}
vector> partition(string s) {
backtracking(s, 0);
return result;
}
};
今天的题目总体难度并不大,主要还是对回溯代码思想的理解,重点在分割回文串这道题,该题细节较多,二刷时应注意。