回溯+深搜(综合版1)

下列题的思想都是深搜+剪枝,代码风格一样,可以看两三个后自己做
没有代码的题,点连接会有代码

电话号码的字母组合

括号生成

组合

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    int nn = 0, kk = 0;
    vector<vector<int>> combine(int n, int k) {
        nn = n;
        kk = k;
        dfs(1);
        return res;
    }
    void dfs(int pos) {
        if (path.size() == kk) {
            res.push_back(path);
            return;
        }
        if (pos > nn)
            return;
        for (int i = pos; i <= nn; ++i) {// jianzhi
            path.push_back(i);
            dfs(i + 1);
            // 恢复现场
            path.pop_back();
        }
    }
};

目标和

// class Solution {
// public:
//     int res = 0;
//     int path = 0;
//     int t = 0;
//     int findTargetSumWays(vector<int>& nums, int target) {
//         t = target;
//         dfs(nums, 0);
//         return res;
//     }
//     void dfs(vector<int>& nums, int pos) {
//         if (pos == nums.size()) {
//             if (path == t)
//                 res++;
//             return;
//         }

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

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

class Solution {
public:
    int res = 0;
    int t = 0;
    int findTargetSumWays(vector<int>& nums, int target) {
        t = target;
        dfs(nums, 0, 0);
        return res;
    }
    void dfs(vector<int>& nums, int pos, int path) {
        if (pos == nums.size()) {
            if (path == t)
                res++;
            return;
        }
        dfs(nums, pos + 1, path + nums[pos]);
        dfs(nums, pos + 1, path - nums[pos]);
    }
};

组合总和

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    int t = 0;
    int n = 0;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        t = target;
        n = candidates.size();
        dfs(candidates, 0, 0);
        return res;
    }
    void dfs(vector<int>& candidates, int pos, int sum) {
        if (sum == t) {
            res.push_back(path);
            return;
        }
        if (sum > t)
            return;
        for (int i = pos; i < n; ++i) {
            path.push_back(candidates[i]);
            dfs(candidates, i, sum + candidates[i]);
            path.pop_back();
        }
    }
};

字母大小全排列

优美的排列

class Solution {
public:
    bool check[16] = {false};
    int res = 0;
    vector<int> path;
    int nums = 0;
    int countArrangement(int n) {
        nums = n;
        dfs();
        return res;
    }
    void dfs() {
        if (path.size() == nums) {
            res++;
            return;
        }
        
        for (int i = 1; i <= nums; ++i) {
            if (!check[i]) {
                if (check1(i, path.size() + 1)) {
                    path.push_back(i);
                    check[i] = true;
                    dfs();
                    path.pop_back();
                    check[i] = false;
                }
            }
        }
    }
    bool check1(int a, int b) {
        if (a % b == 0 || b % a == 0)
            return true;
        return false;
    }
};

N皇后

class Solution {
public:
    vector<string> path;
    vector<vector<string>> res;
    int nums = 0;
    bool col[10] = {false};
    bool dg1[20] = {false};
    bool dg2[20] = {false};
    vector<vector<string>> solveNQueens(int n) {
        nums = n;
        path.resize(n, string(n, '.'));
        dfs(0);
        return res;
    }
    void dfs(int row) {
        if (row == nums) {
            res.push_back(path);
            return;
        }
        for (int i = 0; i < nums; ++i) {
            if (!col[i] && !dg1[i + row] && !dg2[i - row + nums]) {
                path[row][i] = 'Q';
                col[i] = dg2[i - row + nums] = dg1[i + row] = true;
                dfs(row + 1);
                // recovery
                path[row][i] = '.';
                col[i] = dg2[i - row + nums] = dg1[i + row] = false;
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值