【递归、回溯和剪枝】综合训练<一>

1.找出所有子集的异或总和再求和

找出所有子集的异或总和再求和

 版本一:

class Solution {
public:
    int sum;
    int path;
    int subsetXORSum(vector<int>& nums) {
        dfs(nums, 0);
        return sum;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if(pos == nums.size())
        {
            sum += path;
            return;
        }
        //选
        path ^= nums[pos];
        dfs(nums, pos + 1);
        path ^= nums[pos];
        //不选
        dfs(nums, pos + 1);
    }
};

版本二:

class Solution {
public:
    int sum;
    int path;
    int subsetXORSum(vector<int>& nums) {
        dfs(nums, 0);
        return sum;
    }
    void dfs(vector<int>& nums, int pos)
    {
        sum += path;
        for(int i = pos; i < nums.size(); i++)
        {
            path ^= nums[i];
            dfs(nums, i + 1);
            path ^= nums[i];
        }
    }
};

2.全排列ii

全排列ii

剪枝:

 

class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    bool check[8];
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        if(path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for(int i = 0; i < nums.size(); i++)
        {
            //两种剪枝方法
            //只关注不合法情况
            //if(check[i] != false || (i > 0 && nums[i] == nums[i - 1] && check[i - 1] == false))
            //     continue;
            // path.push_back(nums[i]);
            // check[i] = true;
            // dfs(nums, i + 1);
            // path.pop_back();
            // check[i] = false;
            //只关注合法情况
            if((check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1]) != false))
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums, i + 1);
                path.pop_back();
                check[i] = false;
            }
        }
    }
};

3.电话号码的字母组合

电话号码的字母组合

class Solution {
public:
    string hash[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    vector<string> ret;
    string path;
    vector<string> letterCombinations(string digits) {
        if(digits.size() == 0) return ret;
        dfs(digits, 0);
        return ret;
    }
    void dfs(string digits, int pos)
    {
        if(pos == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for(char ch : hash[digits[pos] - '0'])
        {
            path.push_back(ch);
            dfs(digits, pos + 1);
            path.pop_back();//恢复现场
        }
    }
};

4.括号生成

括号生成

有效括号?

 

class Solution {
public:
    int _n;
    vector<string> ret;
    string path;
    int left, right;
    vector<string> generateParenthesis(int n) {
        _n = n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if(right == _n)
        {
            ret.push_back(path);
            return;
        }
        //增加左括号
        if(left < _n)
        {
            path.push_back('(');left++;
            dfs();
            path.pop_back();left--;
        }
        //增加右括号
        if(left > right)
        {
            path.push_back(')'); right++;
            dfs();
            path.pop_back(); right--;
        }
    }
};

5.组合

组合

class Solution {
public:
    int _n, _k;
    vector<vector<int>> ret;
    vector<int> path;
    vector<vector<int>> combine(int n, int k) {
        _n = n;
        _k = k;
        dfs(1);
        return ret;
    }
    void dfs(int pos)
    {
        if(path.size() == _k)
        {
            ret.push_back(path);
            return;
        }
        for(int i = pos; i <= _n; i++)
        {
            path.push_back(i);
            dfs(i + 1);
            path.pop_back();
        }
    }
};

6.目标和

目标和

class Solution {
public:
    int aim;
    int ret;
    int findTargetSumWays(vector<int>& nums, int target) {
        aim = target;
        dfs(nums, 0, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos, int path)
    {
        if(pos == nums.size())
        {
            if(path == aim) ret++;
            return;
        }
        //path做全局变量版本
        // //添加+ 
        // path += nums[pos];
        // dfs(nums, pos + 1);
        // path -= nums[pos];

        // //添加-
        // path -= nums[pos];
        // dfs(nums, pos + 1);
        // path += nums[pos];
        //path做参数版本
        dfs(nums, pos + 1, path + nums[pos]);

        dfs(nums, pos + 1, path - nums[pos]);
        
    }
};

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花影随风_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值