Iterative backtracking solution python


  • 0
    K
    class Solution(object):
        def combinationSum3(self, k, n):
            """
            :type k: int
            :type n: int
            :rtype: List[List[int]]
            """
            
            i = 0
            solution = [0]*(k)
            result = []
            
            while i != -1:
                if i == k:
                    if sum(solution) == n and len(set(solution)) == len(solution):
                        result.append(solution[:])
                    i -= 1
                    continue
            
                solution[i] = solution[i] + 1
                
                if i != 0:
                    if solution[i] <= solution[i-1]:
                        continue
                    
                if solution[i] != 10:
                    i += 1
                else:
                    solution[i] = 0
                    i -=1
            
            return result
    

Log in to reply
 

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