Python Simple Solution

  • 0

    No need to sort actually.

    class Solution(object):
        def combinationSum(self, candidates, target):
            def dfs(candidates, start, target, path, res):
                if target == 0:
                    return res.append(path + [])
                for i in range(start, len(candidates)):
                    if target - candidates[i] >= 0:
                        dfs(candidates, i, target - candidates[i], path, res)
            res = []
            dfs(candidates, 0, target, [], res)
            return res

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.