LeetCoede 39
这里主要是可以有重复的元素,这也就意味着迭代的startIndex不需要+1了
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void findPath(vector<int>& nums, int target, int sum, int startIndex){
if(sum == target){
res.push_back(path);
return;
}
if(sum > target) return;
for(int i=startIndex; i<nums.size(); i++){
sum += nums[i];
path.push_back(nums[i]);
findPath(nums, target, sum, i);
path.pop_back();
sum -= nums[i];
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
findPath(candidates, target, 0, 0);
return res;
}
};
优化后:
这里需要给数组排序(!)
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void findPath(vector<int>& nums, int target, int sum, int startIndex){
if(sum == target){
res.push_back(path);
return;
}
for(int i=startIndex; i<nums.size() && nums[i]+sum <= target; i++){
sum += nums[i];
path.push_back(nums[i]);
findPath(nums, target, sum, i);
path.pop_back();
sum -= nums[i];
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
findPath(candidates, target, 0, 0);
return res;
}
};
*40. Combination Sum II
重点理解如何去重
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void findPath(vector<int>& nums, int target, int sum, int startIndex, vector<bool>& used){
if(target == sum){
res.push_back(path);
return;
}
for(int i=startIndex; i<nums.size() && nums[i]+sum <= target; i++){
if(i>0 && nums[i] == nums[i-1] && used[i-1] == false){
continue;
}
sum += nums[i];
path.push_back(nums[i]);
used[i] = true;
findPath(nums, target, sum, i+1, used);
used[i] = false;
sum -= nums[i];
path.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<bool> used(candidates.size(), false);
findPath(candidates, target, 0, 0, used);
return res;
}
};
*131. Palindrome Partitioning
理解分割和组合的相似之处
class Solution {
public:
vector<vector<string>> res;
vector<string> oneWay;
void findWay(string& s, int startIndex){
if(startIndex >=s.size()){
res.push_back(oneWay);
return;
}
for(int i=startIndex; i<s.size();i++){
if(isPalindrome(s, startIndex, i)){
string str = s.substr(startIndex, i-startIndex+1);
oneWay.push_back(str);
}
else{
continue;
}
findWay(s, i+1);
oneWay.pop_back();
}
}
vector<vector<string>> partition(string s) {
findWay(s, 0);
return res;
}
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;
}
};
二叉树:129. Sum Root to Leaf Numbers
class Solution {
public:
int sum=0;
void traversal(TreeNode* root, int res){
res = res*10+root->val;
if(root->left == NULL && root->right == NULL){
sum += res;
return;
}
if(root->left) traversal(root->left, res);
if(root->right) traversal(root->right, res);
}
int sumNumbers(TreeNode* root) {
traversal(root, 0);
return sum;
}
};
1.res要放在最前面,意味着已进入节点就加一个数字
2.当遇到终止条件为(root->left &&root->right)的时候,后面可能需要if(root->left)来避免进入到root==NULL的节点(也可以在前面加一个root != NULL条件)
3.traversal(root, res)res会自动回溯,不需要在减一次了
使用string的方式
class Solution {
public:
int sum=0;
string s;
void traversal(TreeNode* root){
if(root == NULL) return;
s.append(to_string(root->val));
if(root->left == NULL && root->right == NULL){
sum += stoi(s);
}
traversal(root->left);
traversal(root->right);
s.pop_back();
}
int sumNumbers(TreeNode* root) {
traversal(root);
return sum;
}
};