Python Backtracking


  • 0
    class Solution(object):
        def combinationSum(self, candidates, target):
            answer = []
            def backtracking(idx, cur, ans):
                # print ans
                if cur == target:
                    answer.append(ans)
                    return
                if cur > target:
                    return
                i = idx
                while i < len(candidates):
                    backtracking(i, cur + candidates[i], ans + [candidates[i]])
                    while i + 1 < len(candidates) and candidates[i + 1] == candidates[i]:
                        i += 1
                    i += 1
            backtracking(0, 0, [])
            return answer
    

Log in to reply
 

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