LeetCode(力扣)39. 组合总和Python

LeetCode20. 有效的括号

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/combination-sum/description/
LeetCode(力扣)39. 组合总和Python_第1张图片

代码

class Solution:
    def backtracking(self, candidates, stratindex, path, target, result, total):
        if total > target:
            return
        if total == target:
            result.append(path[:])

        for i in range(stratindex, len(candidates)):
            total += candidates[i]
            path.append(candidates[i])
            self.backtracking(candidates, i, path, target, result, total)
            total -= candidates[i]
            path.pop()
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        self.backtracking(candidates, 0, [], target, result, 0)
        return result

你可能感兴趣的:(leetcode,python,算法,职场和发展)