39. 组合总和
本题没有数量重复,所以和前面组合不同,startIndex每一次递归都是从头开始
当和大于当前元素时,剪枝,为了剪枝需要将数组重新排序
class Solution {
public:
vector<vector<int>> result;
vector<int> nums;
void backtracking(const vector<int>& candidates, int target, int sum, int startIndex) {
if (sum == target) {
result.push_back(nums);
return;
}
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) { //对集合排序后剪枝操作
nums.push_back(candidates[i]);
sum += candidates[i];
backtracking(candidates, target, sum, i); //递归也是从i开始
nums.pop_back();
sum -= candidates[i];
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0);
return result;
}
};
40.组合总和II
此题要求每个元素只能使用一次,而且最后结果的数组都不能重合
所以一个重要的操作就是去重
一个操作是,用一个used数组记录每个元素的使用情况,每一层同样的数字不能重复,但是每一枝相同的数可以重复,保证不是同一个元素就行
采用used数组
class Solution {
public:
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++) {
if(i > 0 && candidates[i] == candidates[i-1] && used[i - 1] == false) {//前一个相同元素已经使用完了,回退为false
continue;
}
path.push_back(candidates[i]);
sum += candidates[i];
used[i] = true;
backtracking(candidates, target, sum, i + 1, used);
used[i] = false;
path.pop_back();
sum -= candidates[i];
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<bool> used(candidates.size(), false);
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0, used);
return result;
}
};
采用startIndex去重
语言: cpp
添加备注
class Solution {
public:
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;
}
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
if(i > startIndex && candidates[i] == candidates[i-1]) {//去重,对每一层递归去重
continue;
}
path.push_back(candidates[i]);
sum += candidates[i];
backtracking(candidates, target, sum, i + 1);
path.pop_back();
sum -= candidates[i];
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backtracking(candidates, target, 0, 0);
return result;
}
};
131.分割回文串
有两个问题,
切割问题:用startIndex当做切割的位置
判断是否是回文串:另写一个判断函数
递归中止条件:当切割到最后时中止
class Solution {
public:
vector<vector<string>> result;
vector<string> path;
void backtracking(const string& s, int startIndex) {
if (startIndex >= s.size()) {
result.push_back(path);
return;
}
for (int i = startIndex; i < s.size(); i++) {
if (isPalindrome(s, startIndex, i)) {
string str = s.substr(startIndex, i - startIndex + 1); //截取子串
path.push_back(str);
} else continue;
backtracking(s, i + 1);
path.pop_back();
}
}
bool isPalindrome(const string& s, int start, int end) {//判断回文子串
for (int i = start, j = end; i < j; i++, j--) {
if (s[i] != s[j]) {
return false;
}
}
return true;
}
vector<vector<string>> partition(string s) {
backtracking(s, 0);
return result;
}
};