39.Combination Sum

25 年 7 月 29 日 星期二
53 字
1 分钟

39. Combination Sum

python
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        ans = []
        path = []
        n = len(candidates)
        # t means left target
        def dfs(i, t):
            if t < 0:
                return
            if t == 0:
                ans.append(path[:])
                return
            for j in range(i, n):
                path.append(candidates[j])
                dfs(j, t-candidates[j])
                path.pop()
        dfs(0, target)
        return ans

文章标题:39.Combination Sum

文章作者:Sirui Chen

文章链接:https://blog.siruichen.me/posts/39combination_sum[复制]

最后修改时间:


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。
本文采用CC BY-NC-SA 4.0进行许可。