Simple Python DFS solution


  • 0
    G
    def getFactors(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        res = []
        path = []
        self.dfs(n, 2, path, res)
        return res
    
    def dfs(self, n, start, path, res):
    
        if path and n >= start:
            path.append(n)
            res.append(list(path))
            path.pop()
        
        for i in range(start, int(n**0.5)+1):
            if n % i == 0:
                path.append(i)
                self.dfs(n//i, i, path, res)
                path.pop()

Log in to reply
 

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