1ms beat 97% dfs


  • 0
    F
    public List<List<Integer>> getFactors(int n) {
            List<List<Integer>> ress = new ArrayList<>();
            if (n < 4) return ress;
            List<Integer> res = new ArrayList<>();
            for (int i = 2; i*i <= n; i++) {
                if (n%i == 0) {
                    res.add(i);
                    dfs(ress, res, i, n/i);
                    res.remove(res.size()-1);
                }
            }
            return ress;
        }
        
        private void dfs(List<List<Integer>> ress, List<Integer> res, int lower, int n) {
            res.add(n);
            ress.add(new ArrayList<>(res));
            res.remove(res.size()-1);
            
            for (int i = lower; i*i <= n; i++) {
                if (n%i == 0) {
                    res.add(i);
                    dfs(ress, res, i, n/i);
                    res.remove(res.size()-1);
                }
            }
        }

Log in to reply
 

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