Python backtracking solution.

  • 3
    def getFactors(self, n):
        res = []
        self.dfs(self.factors(n)[1:-1], n, 0, [], res)
        return res
    def dfs(self, nums, n, index, path, res):
        tmp = reduce(lambda x,y:x*y, path, 1)
        if tmp > n:
            return  # backtracking
        if tmp == n and path:
            return  # backtracking 
        for i in xrange(index, len(nums)):
            self.dfs(nums, n, i, path+[nums[i]], res)
    def factors(self, n):
        res = []
        for i in xrange(1, n+1):
            if n % i == 0:
        return res

Log in to reply

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