My java solution


  • 0
    O
    public class Solution {
        public List<List<Integer>> getFactors(int n) {
            List<List<Integer>> list = new ArrayList<>();
            List<Integer> curr = new ArrayList<>();
            curr.add(n);
            helper(list, curr);
            return list;
        }
        
        public void helper(List<List<Integer>> list, List<Integer> curr) {
            int num = curr.get(curr.size() - 1);
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    curr.remove(curr.size() - 1);
                    if (curr.size() == 0 || (i >= curr.get(curr.size() - 1) && num / i >= i)) {
                        curr.add(i);
                        curr.add(num / i);
                        list.add(new ArrayList<Integer>(curr));
                        helper(list, curr);
                        curr.remove(curr.size() - 1);
                        curr.remove(curr.size() - 1);
                        
                        }
                        curr.add(num);
                    }
            }
            return;
        }
    }
    

Log in to reply
 

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