【力扣100】39.组合总和

添加链接描述

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def backtrack(path,target,res,index):
            if target==0:
                res.append(path[:])
                return
            if target<0:
                return 
            for i in range(index,len(candidates)):
                if target>=candidates[i]:
                    path.append(candidates[i])
                    backtrack(path,target-candidates[i],res,i)
                    path.pop()
        res=[]
        backtrack([],target,res,0)
        return res

思路:

  1. 递归与回溯,一眼树形结构,找到想要的节点就可以
    【力扣100】39.组合总和_第1张图片

你可能感兴趣的:(leetcode,python,算法)