二刷254. Factor Combinations

Medium
我一刷的时候写的是”似懂非懂”, 然后二刷的时候就没做出来。彻底理解一道题比刷完重要很多,刷题一定要重视质量。我是因为最近要面试了狂刷tag题过得比较快,但重点题还是得扣细节.

class Solution {
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfsHelper(res, list, 2, 1, n);
        return res;
    }
    
    private void dfsHelper(List<List<Integer>> res, List<Integer> list, int startNum, int product, int n){
        if (startNum > n || product > n){
            return;
        }        
        if (product == n){
            //Q? when to use Arrays.asList()
            res.add(new ArrayList<>(list));
            return;  
        }
        for (int i = startNum; i < n; i++){
            if (i*product > n){
                break;
            }
            if (n % i == 0){
                list.add(i);
                dfsHelper(res, list, i, product * i, n);
                list.remove(list.size() - 1);
            }
        }
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/4639b3dc413e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞