Simple python iterative backtracking solution


  • 12
    def getFactors(self, n):
        ans, stack, x = [], [], 2
        while True:
            if x > n / x:
                if not stack:
                    return ans
                ans.append(stack + [n])
                x = stack.pop()
                n *= x
                x += 1
            elif n % x == 0:
                stack.append(x)
                n /= x
            else:
                x += 1
    
    
    # 20 / 20 test cases passed.
    # Status: Accepted
    # Runtime: 40 ms
    # 97.25%

Log in to reply
 

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