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 function that takes an integer n and return all possible combinations of its factors.
Note:
- Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is
[2, 6]
, not[6, 2]
. - You may assume that n is always positive.
- 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]
]
题解:
求一个数的所有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的更多相关文章
- 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 ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- [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 ...
- [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 ...
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- [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 ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [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 ...
- [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 ...
随机推荐
- TClientDataSet中关于TField、TFieldDef动态创立字段的应用
//使用 TFieldDef 建表: begin with ClientDataSet1.FieldDefs do begin Add('Name' , ftString, 12, True); { ...
- MySQL 5.7.11 重置root密码
.修改/etc/my.conf,添加参数skip-grant-tables .重启mysql service mysqld stop service mysqld start .用root 直接登录 ...
- 内部技术分享的 PPT
本文的基础是搞了一次内部的技术分享,在此也分享一下本次的PPT的一些内容.先列一下大概内容吧. EF-Code First API(WCF.WebAPI) Xaml MVVM AOP Xamarin. ...
- nginx服务器绑定域名和设置根目录
首先进入nginx安装目录的配置目录conf,然后执行 vi conf/nginx.conf 打开nginx的配置文件,找到并修改红字部分 server { listen default_server ...
- Teradata中fastload使用
Teradata Fastload Utility 是teradata数据库中一个基于命令行的快速load大量数据到一个空表的工具. 数据可以从以下途径被load: 1) Disk 或 tape; 2 ...
- Linux命令练级初级
Linux命令练级初级 http://ke.qq.com/video/index.html?course_id=6815 ls -a 所有文件 -l 类型,连接数,所属用户,工作组, ...
- HDU3887 DFS序+ 线段树
查询树上某个节点的子节点的标号小于其标号的数目. 一个trick是建立线段树之后,从标号小的向标号大的来做更新. 1: #include <cstdio> 2: #include < ...
- UI控件tag属性和魔法数字的处理
说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBActio ...
- OC类的本质,深入探讨,load方法和initialize方法
1:类的本质:类也是一种类,可以叫做类类,类对象,类类型: 2:类和对象在内存中分配问题(注意区分类的对象和类对象的概念) 类对象在内存中只有一份,且只加载一次,类对象中存放了类中定义的方法: 而成员 ...
- c语言基础:各种数据类型的输出占位符
c语言中的输出操作相对java来说是比较麻烦的,每种数据类型的输出都有各自的占位符: 下面是各种数据类型的输出占位符: short/int : %d ; printf("这个整数是:%d&q ...