码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Factor Combinations

时间:2015-11-26 17:10:15      阅读:162      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!