Python easy to understand solution


  • 0
    class Solution(object):
        def getFactorsHelper(self, ans, notail, n):
            i = 2
            if len(notail) > 0:
                i = notail[-1]
            while i ** 2 <= n:
                if n % i == 0:
                    j = n / i
                    if not notail:
                        ans.append([i, j])
                    else:
                        # we want to keep original notail for the next while loop
                        _origin = notail + [i, j]
                        ans.append(_origin)
                    
                    if j <= 3:
                        break
                    else:
                        # separate the last index(tail) and others(notail)
                        _origin = notail + [i]
                        self.getFactorsHelper(ans, _origin, j)
                i += 1
            
        
        def getFactors(self, n):
            ans = []
            notail = [] 
            self.getFactorsHelper(ans, notail, n)
            return ans

Log in to reply
 

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