Share simple Python solution


  • 6
    D
    class Solution(object):
        def getFactors(self, n):
            """
            :type n: int
            :rtype: List[List[int]]
            """
            
            if n <= 1:
                return []
            
            res = []
            
            i = 2
            while i * i <= n:
                if n % i == 0:
                    q = n / i
                    res.append([i, q])
                    subres = self.getFactors(q)
                    for r in subres:
                        if r[0] >= i:
                            res.append([i] + r)
                i += 1
                    
            return res

  • 0
    I

    when you go through all the subresults, how come you check if r[0] >= i?


  • 0
    P

    @IWantToPass if r[0] >= i: is used for checking and avoiding duplicates, for example when n=24, only [2,2,2,3]should be recorded, not [2,3,2,2]


Log in to reply
 

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