Thinking process

If it is able to evenly divided the number, add to list.

Mistakes

only add the list to result when the size is larger than 1

public class Solution {
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> result = new ArrayList<>();

        dfsHelper(result, new ArrayList<Integer>(), n, 2);

        return result;
    }

    private void dfsHelper(List<List<Integer>> result, List<Integer> list, int n, int start) {
        if (n <= 1) {
            if (list.size() > 1) {
                result.add(new ArrayList<>(list));
            }
            return;
        }

        for (int i = start; i <= n; ++i) {
            if (n % i == 0) {
                System.out.println(n);
                list.add(i);
                dfsHelper(result, list, n / i, i);
                list.remove(list.size() - 1);
            }
        }
    }
}

results matching ""

    No results matching ""