题目:

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.

Examples: 
input: 1
output:

[]

input: 37
output:

[]

input: 12
output:

[
[2, 6],
[2, 2, 3],
[3, 4]
]

input: 32
output:

[
[2, 16],
[2, 2, 8],
[2, 2, 2, 4],
[2, 2, 2, 2, 2],
[2, 4, 4],
[4, 8]
]

链接: http://leetcode.com/problems/factor-combinations/

题解:

求一个数的所有factor,这里我们又想到了DFS + Backtracking, 需要注意的是,factor都是>= 2的,并且在此题里,这个数本身不能算作factor,所以我们有了当n <= 1时的判断 if(list.size() > 1) add the result to res.

Time Complexity - O(2n), Space Complexity - O(n).

public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
getFactors(res, list, n, 2);
return res;
} private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int factor) {
if(n <= 1) {
if(list.size() > 1)
res.add(new ArrayList<Integer>(list));
return;
} for(int i = factor; i <= n; i++) {
if(n % i == 0) {
list.add(i);
getFactors(res, list, n / i, i);
list.remove(list.size() - 1);
}
}
}
}

二刷:

还是使用了一刷的办法,dfs + backtracking。但递归结束的条件更新成了n == 1。 但是速度并不是很快,原因是没有做剪枝。我们其实可以设置一个upper limit,即当i > Math.sqrt(n)的时候,我们不能继续进行下一轮递归,此时就要跳出了。

Java:

public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
if (n <= 1) return res;
getFactors(res, new ArrayList<>(), n, 2);
return res;
} private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int pos) {
if (n == 1) {
if (list.size() > 1) res.add(new ArrayList<>(list));
return;
}
for (int i = pos; i <= n; i++) {
if (n % i == 0) {
list.add(i);
getFactors(res, list, n / i, i);
list.remove(list.size() - 1);
}
}
}
}

Update: 使用@yuhangjiang的方法,只用计算 2到sqrt(n)的这么多因子,大大提高了速度。

public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
if (n <= 1) return res;
getFactors(res, new ArrayList<>(), n, 2);
return res;
} private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int pos) {
for (int i = pos; i <= Math.sqrt(n); i++) {
if (n % i == 0 && n / i >= i) {
list.add(i);
list.add(n / i);
res.add(new ArrayList<>(list));
list.remove(list.size() - 1);
getFactors(res, list, n / i, i);
list.remove(list.size() - 1);
}
}
}
}

Reference:

https://leetcode.com/discuss/51261/iterative-and-recursive-python

https://leetcode.com/discuss/87926/java-2ms-easy-to-understand-short-and-sweet

https://leetcode.com/discuss/58828/a-simple-java-solution

https://leetcode.com/discuss/72224/my-short-java-solution-which-is-easy-to-understand

https://leetcode.com/discuss/82087/share-bit-the-thought-process-short-java-bottom-and-top-down

254. 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. 254. Factor Combinations 返回所有因数组合

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

  3. [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 ...

  4. [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 ...

  5. Factor Combinations

    Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...

  6. [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 ...

  7. LeetCode Factor Combinations

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. jqueryGrid 内置的onclickSubmit afterSubmit

    $(document).ready(function() { $('#jpgCustomers').jqGrid({ //url from wich data should be requested ...

  2. Windows7鼠标右键里没有新建文本文件的选项,解决办法

    1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...

  3. 机器学习实战——k-邻近算法:约会网站

    1.kNN 算法 算法说明: set<X1,X2……Xn> 为已知类别数据集,预测 点Xt 的类别: (1)计算中的set中每一个点与Xt的距离 (2)按距离增序排列 (3)选择距离最小的 ...

  4. VirtualBox中虚拟Ubuntu添加新的虚拟硬盘

    VirtualBox中装好Ubuntu后,发现硬盘空间不够使用 了.以下是搜集整理的解决办法: 1. 添加新硬盘 设置 -> Storage -> SATA控制器->右击,选择&qu ...

  5. static方法不能直接访问类内的非static变量和不能调用this,super语句分析

    大家都知道在static方法中,不能访问类内非static成员变量和方法.可是原因是什么呢? 这首先要从static方法的特性说起.static方法,即类的静态成员经常被称为"成员变量&qu ...

  6. VirtualBox虚拟机安装MSDOS和MINIX2.0.0双系统

    1. 在VirtualBox中新建一个MSDOS虚拟机. 2.下载一个MSDOS软盘镜像. 3.启动虚拟机,提示选择安装盘时,选择步骤2下载过来的MSDOS镜像. 4.正常启动进入DOS命令行,用FD ...

  7. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据

    jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...

  8. Java Day 02

    关键字 都是小写,类名首字母大写 标识符 1.数字不可以开头 2.不可以使用关键字 区分大小写 26个大小写字母.0-9._.$ 组成 main是关键字么? 注释 单行注释 // 多行注释 /* */ ...

  9. VBS基础篇 - 常量

    常量:指的是在程序运行过程中其值保持不变的量,它用来保存固定不变的数值,字符串等常数 . 常量的定义:在vbscript中使用使用 Const 指令可以创建名称具有一定含义的字符串型或数值型常量,并给 ...

  10. C# 连接 Oracle 的几种方式[转]

    本文转自:http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html 一:通过System.Data.OracleClient(需要安装 ...