Coding test/programmers - single

[leetcode] 39. Combination Sum

engine 2022. 4. 1. 16:27
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        def dfs(cansum,index,path):
            if cansum < 0 :
                return
            if cansum == 0 :
                result.append(path)
                return
            for i in range(index,len(candidates)):
                dfs(cansum - candidates[i],i, path + [candidates[i]])
        dfs(target, 0, [])
        return result