Actually sweet and actually simple


  • 0
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> result = new ArrayList <>();
        if ( n <= 1) return result;
            
        for ( int i = 2 ; i * i <= n ; i ++){
           if (n % i == 0 && i <= n/i) {
                result.add (Arrays.asList(i, n/i));
                    
                for (List<Integer> c : getFactors(n/i)) {    
                    if (i <= c.get(0)) {
                            
                        List<Integer> p = new ArrayList<>();
                        p.add (i);
                        p.addAll(c);
                        result.add (p);
                     }
                }
            }
        }
       return result;
    }
    

Log in to reply
 

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