立志重刷代码随想录60天冲冲冲!!——第二十三天

39. 组合总和

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    int sum = 0;
    void backtracking(vector<int> candidates, int target, int startIdx) {
        if (sum == target) {
            res.push_back(path);
            return;
        } else if (sum > target) { // sum大于target也需要返回!!!
            return;
        }

        for (int i = startIdx; i < candidates.size(); i++) {
            int num = candidates[i];
            path.push_back(num);
            sum += num;
            backtracking(candidates, target, i); // 不需要加一
            sum -= num;
            path.pop_back();
        }
        return;
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        backtracking(candidates, target, 0);
        return res;
    }
};

40.组合总和II

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    int sum = 0;
    void backtracking(vector<int> candidates, int target, int startIdx, vector<bool>& used) {
        if (sum > target) return;
        else if (sum == target) {
            res.push_back(path);
            return;
        }

        for (int i = startIdx; i < candidates.size(); i++) {
            // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
            // used[i - 1] == false,说明同一树层candidates[i - 1]使用过
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) continue;

            int num = candidates[i];
            path.push_back(num);
            sum += num;
            used[i] = true;
            backtracking(candidates, target, i + 1, used);
            used[i] = false;
            sum -= num;
            path.pop_back();
        }
        return;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        // 初始化bool数组
        vector<bool> used(candidates.size(), false);
        sort(candidates.begin(), candidates.end());
        backtracking(candidates, target, 0, used);
        return res;
    }
};

131.分割回文串

s.substr(起始索引,个数)

class Solution {
public:
    vector<vector<string>> res;
    vector<string> path;

    bool isCycleTring(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;
    }

    void backtracking(string s, int startIdx) {
        if (s.size() == startIdx) {
            res.push_back(path);
            return;
        }

        for (int i = startIdx; i < s.size(); i++) {
            if (isCycleTring(s, startIdx, i) == true) {
                string str = s.substr(startIdx, i - startIdx + 1); //s.substr(起始索引,个数);
                path.push_back(str);
            } else continue;

            backtracking(s, i + 1);
            path.pop_back();
        }
        return;
    }
    vector<vector<string>> partition(string s) {
        backtracking(s, 0);
        return res;
    }
};

代码随想录更新 文章被收录于专栏

冲冲冲冲冲冲!

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务