代码随想录算法训练营Day27 | 39. 组合总和,40.组合总和II,131.分割回文串

39. 组合总和

文章链接  |  题目链接  |  视频链接

C++链接

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int start, int sum){
        if (sum > target){
            return;
        } else if (sum == target){
            result.push_back(path);
            return;
        }
        for (int i = start; i < candidates.size(); i++){
            path.push_back(candidates[i]);
            backtracking(candidates, target, i, sum+candidates[i]);
            path.pop_back();
        }
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        backtracking(candidates, target, 0, 0);
        return result;
    }
};

Python链接

class Solution:
    def backtracking(self, candidates: List[int], target: int, result: List[List[int]], comb: List[int], currSum: int, start: int):
        print(currSum)
        if currSum > target:
            return
        if currSum == target:
            result.append(comb[:])
            return

        for i in range(start, len(candidates)):
            comb.append(candidates[i])
            self.backtracking(candidates, target, result, comb, currSum+candidates[i], i)
            comb.pop()

    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        self.backtracking(candidates, target, result, [], 0, 0)
        return result

40.组合总和II

文章链接  |  题目链接  |  视频链接

C++链接

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& candidates, int target, int start, int sum){
        if (sum > target){
            return;
        } else if (sum == target){
            result.push_back(path);
            return;
        }
        for (int i = start; i < candidates.size(); i++){
            if (i > start && candidates[i] == candidates[i-1]){
                continue;
            }
            path.push_back(candidates[i]);
            backtracking(candidates, target, 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 result;
    }
};

Python链接

class Solution:
    def backtracking(self, candidates: List[int], target:int, sum: int, start: int, result: List[List[int]], path: List[int]):
        if sum > target:
            return
        if sum == target:
            result.append(path[:])
            return
        for i in range(start, len(candidates)):
            if (i > start and candidates[i] == candidates[i-1]):
                continue
            path.append(candidates[i])
            self.backtracking(candidates, target, sum+candidates[i], i+1, result, path)
            path.pop()

    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        candidates.sort()
        self.backtracking(candidates, target, 0, 0, result, [])
        return result

131.分割回文串

文章链接  |  题目链接  |  视频链接

C++链接

class Solution {
public:
    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;
    }
    vector<vector<string>> result;
    vector<string> path;
    void backtracking(const string&s, int start){
        if (start >= s.size()){
            result.push_back(path);
            return;
        }
        for (int i = start; i < s.size(); i++){
            if (isPalindrome(s, start, i)){
                string str = s.substr(start, i - start + 1);
                path.push_back(str);
            } else {
                continue;
            }
            backtracking(s, i+1);
            path.pop_back();
        }
    }
    vector<vector<string>> partition(string s) {
        backtracking(s, 0);
        return result;
    }
};

Python链接

class Solution:
    def isPalindrome(self, s: str) -> bool:
        return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2))

    def partition(self, s: str) -> List[List[str]]:
        result = []
        self.partition_helper(s, 0, [], result)
        return result

    def partition_helper(self, s, start_index, path, result):
        if start_index == len(s):
            result.append(path[:])
            return

        for i in range(start_index + 1, len(s) + 1):
            sub = s[start_index:i]
            if self.isPalindrome(sub):
                path.append(sub)
                self.partition_helper(s, i, path, result)
                path.pop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值