39. 组合总和
https://leetcode.cn/problems/combination-sum/
本题没什么特别的,和前面的组合问题有点像。
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backTracking(vector<int>& candidates, int target, int cur) {
if(target == 0){
result.push_back(path);
return;
}
if(target < 0) return;
for(int i = cur; i < candidates.size(); i++) {
path.push_back(candidates[i]);
backTracking(candidates, target - candidates[i], i);
path.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
backTracking(candidates, target, 0);
return result;
}
};
40.组合总和II
https://leetcode.cn/problems/combination-sum-ii/
本题和前一题有点类似,但麻烦的是去重
要去重的话有如下几种方法
1.使用一个set容器来记录已经出现过的元素
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backTracking(vector<int> & candidates, int target, int cur) {
if(target == 0) {
result.push_back(path);
return;
}
if(target < 0) return;
unordered_set<int> set;
for(int i = cur; i < candidates.size(); i++) {
if (set.find(candidates[i]) != set.end()) continue;
set.insert(candidates[i]);
path.push_back(candidates[i]);
backTracking(candidates, target - candidates[i], i + 1);
path.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backTracking(candidates, target, 0);
return result;
}
};
2.使用一个vector<bool>来记录元素是否使用过
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backTracking(vector<int> & candidates, int target, int cur, vector<bool> & used) {
if(target == 0) {
result.push_back(path);
return;
}
if(target < 0) return;
for(int i = cur; i < candidates.size(); i++) {
if(i > 0 && candidates[i] == candidates[i-1]){
if(used[i-1] == false) continue;
}
path.push_back(candidates[i]);
used[i] = true;
backTracking(candidates, target - candidates[i], i + 1, used);
path.pop_back();
used[i] = false;
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<bool> used(candidates.size(), false);
sort(candidates.begin(), candidates.end());
backTracking(candidates, target, 0, used);
return result;
}
};
3.和前面一个used差不多,直接在当前元素节点上比较
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backTracking(vector<int> & candidates, int target, int cur) {
if(target == 0) {
result.push_back(path);
return;
}
if(target < 0) return;
for(int i = cur; i < candidates.size(); i++) {
if(i > cur && candidates[i] == candidates[i-1]) continue;
path.push_back(candidates[i]);
backTracking(candidates, target - candidates[i], i + 1);
path.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
backTracking(candidates, target, 0);
return result;
}
};
131.分割回文串
https://leetcode.cn/problems/palindrome-partitioning/
不断尝试用各种方法分割字符串,如果左串是回文串,那就将右串作为新的字符串继续循环,直到分无可分为止
直接操作字符串:
class Solution {
public:
bool isPalindromic(string s) {
for(int i = 0; i < s.length() / 2; i++){
if (s[i] != s[s.length() - i - 1]) return false;
}
return true;
}
vector<vector<string>> result;
vector<string> path;
void backTracking(string s) {
if (s.empty()) {
result.push_back(path);
return;
}
for(int i = 0; i < s.length(); i++) {
string left(s.begin(), s.begin() + i + 1);
if (!isPalindromic(left)) continue;
path.push_back(left);
string right(s.begin() + i + 1, s.end());
backTracking(right);
path.pop_back();
}
}
vector<vector<string>> partition(string s) {
backTracking(s);
return result;
}
};
利用下标:
class Solution {
public:
bool isPalindromic(string s) {
for(int i = 0; i < s.length() / 2; i++){
if (s[i] != s[s.length() - i - 1]) return false;
}
return true;
}
vector<vector<string>> result;
vector<string> path;
void backTracking(string s) {
if (s.empty()) {
result.push_back(path);
return;
}
for(int i = 0; i < s.length(); i++) {
string left(s.begin(), s.begin() + i + 1);
if (!isPalindromic(left)) continue;
path.push_back(left);
string right(s.begin() + i + 1, s.end());
backTracking(right);
path.pop_back();
}
}
vector<vector<string>> partition(string s) {
backTracking(s);
return result;
}
};