Factor Combinations

Problem:

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:

  1. Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2].
  2. You may assume that n is always positive.
  3. Factors should be greater than 1 and less than n.
 public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> listAll = new ArrayList<>();
if (n < ) return listAll;
List<Integer> list = factors(n); helper(n, , , list, listAll, new ArrayList<Integer>());
return listAll;
} public void helper(int n, long value, int index, List<Integer> list, List<List<Integer>> listAll, List<Integer> temp) {
if (index >= list.size() || value >= n) return; int curValue = list.get(index);
temp.add(curValue);
value *= curValue; if (n == value) {
listAll.add(new ArrayList<Integer>(temp));
} helper(n, value, index, list, listAll, temp);
value /= curValue;
temp.remove(temp.size() - );
helper(n, value, index + , list, listAll, temp);
} public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = ; i <= (n + ) / ; i++) {
if (n % i == ) {
list.add(i);
}
}
return list;
}
}

上面代码其实可以用另下面这种方法,原理一样。

 public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
helper(, , n, result, list);
return result;
} public void helper(int start, int product, int n, List<List<Integer>> result, List<Integer> curr) {
if (start > n || product > n) return; if (product == n) {
List<Integer> t = new ArrayList<>(curr);
result.add(t);
} for (int i = start; i <= n / ; i++) {
if(i * product > n) break;
if (n % i == ) {
curr.add(i);
helper(i, i * product, n, result, curr);
curr.remove(curr.size() - );
}
}
}
}

Factor Combinations的更多相关文章

  1. Leetcode 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  2. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

  3. 254. Factor Combinations

    题目: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a ...

  4. [Locked] Factor combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  5. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  6. [Swift]LeetCode254.因子组合 $ Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  7. 254. Factor Combinations 返回所有因数组合

    [抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...

  8. [leetcode]254. Factor Combinations因式组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  9. [LeetCode] 254. Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

随机推荐

  1. Yii rules常用规则(转)

    public function rules() { return array( //必须填写 array('email, username, password,agree,verifyPassword ...

  2. matlab求解相关系数

    最近收到一项新任务,要求两个矩阵的相关系数,说白了就是转换成向量两两计算.本来这个工作我是想自己写个小程序搞定的,但是大家纷纷反映matlab自带了此项功能,本着活到老学到老的心态,我开始查找这个函数 ...

  3. Todd's Matlab讲义第3讲:牛顿法和for循环

    方程数值求解 下面几讲,我们将聚集如下方程的解法: \begin{equation} f(x)=0 \tag{3.1}\label{3.1} \end{equation} 在微积分课程中,我们知道,许 ...

  4. PHP基础 之 基本数据类型练习

    <h3>PHP基础练习</h3> <?php echo "<h4>常量</h4>"; //定义:一般大写,使用下划线间隔 de ...

  5. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  6. TP3.1 中URL和APP区别

    1.__URL__指当前模块地址,就是当前的action的地址.(每个__action都是一个模块)    eg:当前打开config.html,那么config.html里边的__URL__/sav ...

  7. mybatis批量插入数据到oracle

    mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“  错误解决方法 oracle批量插入使用 insert a ...

  8. View和ViewImage设置图片

    1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...

  9. linux 下安装tomcat

    1.把安装包放到 tomcat的目录, 2.然后  解压  tar -zxvf apache-tomcat-7.0.70.tar.gz    解压. 3.然后启动tomcat

  10. Codeforces 260 B. Fedya and Maths

    题目链接:http://codeforces.com/contest/456/problem/B 解题报告:输入一个n,让你判断(1n + 2n + 3n + 4n) mod 5的结果是多少?注意n的 ...