跳转至

Day22 | Part3

约 53 个字 67 行代码 预计阅读时间 1 分钟

39.组合总和

抽象成树形结构,如图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
vector<vector<int>> res;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int idx) {
    if(sum > target) return;
    if(sum == target) {
        res.push_back(path);
        return;
    }
    for(int i = idx; i < candidates.size(); i ++) { 
        sum += candidates[i];
        path.push_back(candidates[i]);
        backtracking(candidates, target, sum, i); // i不需要+1了
        sum -= candidates[i];
        path.pop_back();
    }
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
    backtracking(candidates, target, 0, 0);
    return res;
}

40.组合总和II

与上题,vector中有重复,但不能有重复的组合,即要去重

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
vector<vector<int>> res;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int idx) {
    if(sum > target) return;
    if(sum == target) {
        res.push_back(path);
        return;
    }
    for(int i = idx; i < candidates.size(); i ++) {
        if(i > idx && candidates[i] == candidates[i - 1]) continue;  // 去重
        sum += candidates[i];
        path.push_back(candidates[i]);
        backtracking(candidates, target, sum, i + 1);
        sum -= candidates[i];
        path.pop_back();
    }
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
    sort(candidates.begin(), candidates.end()); // 需要排序,相同元素挨在一起
    backtracking(candidates, target, 0, 0);
    return res;
}

131.分割回文串

树形结构示意图 → 参考

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vector<vector<string>> res;
vector<string> path;
bool help(string s, int l, int r) { //判断[l,r]是回文吗
    for(int i = l, j = r; i < j; i ++, j --)
        if(s[i] != s[j]) return false;
    return true;
}
void backtracking(string s, int idx) {
    if(idx >= s.size()) {
        res.push_back(path);
        return;
    }
    for(int i = idx; i < s.size(); i ++) {
        if(help(s, idx, i)) { // 判断是回文吗? 获取[idx, i]在s中的字串
            string str = s.substr(idx, i - idx + 1); //第二个参数是长度
            path.push_back(str);
        } else continue;
        backtracking(s, i + 1);
        path.pop_back();
    }
}
vector<vector<string>> partition(string s) {
    backtracking(s, 0);
    return res;
}

颜色主题调整

快来和我聊天~

可以的话请给我赞和 star喔~    =>  GitHub stars

评论