Simple Java solution 95.83%


  • 0
    A
    public class Solution {
        public List<List<Integer>> getFactors(int n) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            helper(result, n, new ArrayList<Integer>(), 2);
            return result;
        }
        
        public void helper(List<List<Integer>> result, int n, List<Integer> list, int f) {
            if (n == 1)
                return;
            if (list.size() > 0) {
                list.add(n);
                result.add(new ArrayList<Integer>(list));
                list.remove(list.size() - 1);
            }
            for (int i = f; i * i <= n; i++) {
                if (n % i == 0) {
                    list.add(i);
                    helper(result, n / i, list, i);
                    list.remove(list.size() - 1);
                }
            }
        }
    }
    

Log in to reply
 

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