首页 > 其他 > 详细

[LeetCode]Factor Combinations

时间:2015-11-26 17:08:01      阅读:305      评论:0      收藏:0      [点我收藏+]

这个题目比较要注意的一点是如何防止重复。在dfs中,下一层的因子应该大于等于上一层的

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

 

[LeetCode]Factor Combinations

原文:http://www.cnblogs.com/vision-love-programming/p/4998120.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!