clean Java solution


  • 0
    N
    public List<List<Integer>> getFactorsInternal(int n, int start) {
        List<List<Integer>> list = new ArrayList<>();
        for (int d1 = start; d1 <= (int)Math.sqrt(n); d1++) {
            if (n % d1 == 0) {
                int d2 = n / d1;
                
                List<List<Integer>> d1List = new ArrayList<>();
                d1List.add(Arrays.asList(d1));
                List<List<Integer>> d2List = getFactorsInternal(d2, d1);
                d2List.add(Arrays.asList(d2));
                for (int i = 0; i < d1List.size(); i++) {
                    for (int j = 0; j < d2List.size(); j++) {
                        List<Integer> factors = new ArrayList<>();
                        factors.addAll(d1List.get(i));
                        factors.addAll(d2List.get(j));
                        list.add(factors);
                    }
                }
            }
        }
        
        return list;
    }
    
    public List<List<Integer>> getFactors(int n) {
        return getFactorsInternal(n, 2);
    }

Log in to reply
 

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