leetcood学习笔记-39-组合总和

题目描述:

 方法一:

class Solution:
    def combinationSum(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int

            :rtype: List[List[int]]
            """
            ans=[]
            n=len(candidates)
            if candidates==[]: return []
            for i in range(n):
                if candidates[i]==target:
                    ans.append([candidates[i]])
                elif candidates[i]<target:
                    l=self.combinationSum(candidates[i:],target-candidates[i])
                    for x in l:
                        x.append(candidates[i])
                    ans+=l
            return ans

猜你喜欢

转载自www.cnblogs.com/oldby/p/10577388.html