Javascript Recursive Solution


  • 0
    L
    /**
     * @param {number} n
     * @return {number[][]}
     */
    var getFactors = function(n) {
        const ans = []
        
        const helper = (sofar, n, start) => {
            if (n <= 1) {
                if (sofar.length > 1) {
                    ans.push(sofar.concat())  // a clone of sofar array
                    return
                } else {
                    return []
                }
            }
            
            for (var i=start; i<=n; i++) {
                if (n % i === 0) {
                    sofar.push(i)
                    helper(sofar, n/i, i)
                    sofar.pop()
                }
            }
        } // helper
        
        helper([], n, 2)
        return ans 
    };
    

Log in to reply
 

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