Share my concise JAVA code, 13 lines, easy to understand


  • 0
    L
    public class Solution {
        private List<List<Integer>> getFactors(int n, int minFactor) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            for (int i = minFactor; i <= n / i; i++) {
                if (n % i == 0) {
                    result.add(new ArrayList<>(Arrays.asList(i, n/i)));
                    List<List<Integer>> subResult = getFactors(n/i, i);
                    for (List<Integer> subList : subResult) {
                        subList.add(0, i);
                        result.add(subList);
                    }
                }
            }
            return result;
        }
        
        public List<List<Integer>> getFactors(int n) {
            return getFactors(n, 2);
        }
    }
    

Log in to reply
 

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