AUG!!! Beats 99.8% JAVA


  • 1
    public class Solution {
        public List<List<Integer>> getFactors(int n) {
            List<List<Integer>> result = new ArrayList<>();
            List<Integer> current = new ArrayList<>();
            getFactorsHelper(result, n, current);
            return result;
        }
        public void getFactorsHelper(List<List<Integer>> result, int n, List<Integer> previous) {
            if (n <= 1) {
                return; 
            }
            ArrayList<Integer> current = new ArrayList<>(previous);
            int preNum = (current.isEmpty() ? 2 : current.get(current.size() - 1));
            for (int i = preNum; i*i <= n; i++) {
                if (n % i == 0) {
                    current.add(i);
                    getFactorsHelper(result, n/i, current);
                    current.remove(current.size() - 1);
                }
            }
            if (current.size() != 0) {
                current.add(n);
                result.add(current);
            }
        }
    }
    

Log in to reply
 

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