clueless coding // TODO: be smarter

LeetCode Factor Combinations: Java Solution



TL;DR Code Solution



public void factors(List<List<Integer>> res, List<Integer> cur, int start, int num) {

    if (num == 1) {
        if (cur.size() > 1) {
            res.add(new ArrayList<Integer>(cur));
            return;
        }
    }

    for (int i = start; i <= num; i++) {
        if (num % i == 0) {
            cur.add(i);
            factors(res, cur, i, num / i);
            cur.remove(cur.size() - 1);
        }
    }




}

public List<List<Integer>> getFactors(int n) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    factors(res, new ArrayList<Integer>(), 2, n);
    return res;
}