标签:
Numbers can be regarded as product of its factors. For example,
8 = 2 x 2 x 2; = 2 x 4.
Write a function that takes an integer n and return all possible combinations of its factors.
Note:
[2, 6]
, not [6, 2]
. 1 public List<List<Integer>> getFactors(int n) { 2 List<List<Integer>> listAll = new ArrayList<>(); 3 if (n < 2) return listAll; 4 5 helper(n, 2, 1, listAll, new ArrayList<Integer>()); 6 return listAll; 7 } 8 9 public void helper(int n, int number, int value, List<List<Integer>> listAll, List<Integer> list) { 10 if (number > (n + 1) / 2 || value > n) return; 11 list.add(number); 12 value *= number; 13 14 if (n == value) { 15 listAll.add(new ArrayList<Integer>(list)); 16 } 17 18 helper(n, number, value, listAll, list); 19 value /= number; 20 list.remove(list.size() - 1); 21 helper(n, number + 1, value, listAll, list); 22 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5739783.html