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 ...
随机推荐
- Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境
我的Windows 8.1 环境 1.下载安装Python 2.7.6 在Python官方网站中下载Python2.7.6的Windows安装包,根据默认配置安装到C:\Python27目录. 安装完 ...
- Sublime Text博客插件 --- iblog
iblog是一款 sublime 博客插件,目前只支持cnblog. 项目地址:https://github.com/iskeeter/iblog 功能介绍 新建和更新cnblog的博客 支持mark ...
- JPA学习---第一节:JPA详解
一.详解 JPA JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是 ...
- WPF 多线程处理(5)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- oracle merge into 语法
MERGE INTO upperLowerLimitData t1 USING (select name,enname,starttime,value ... from dual) t2 ON ( ...
- sqlserver oracle 时间加减
sqlserver: 减去10小时: dateadd(hour,-10,Your_date) oracle : 减去10小时: sysdate()-10/24
- AFNetworking VS ASIHTTPRequest
AFNetworking和ASIHTTPRequest,大致如下: 使用上:AFN是用上较ASI略简单,但扩展不如ASI;AFN能按普通的block写法直接用闭包的写法,但是ASI不行,这样ASI的代 ...
- Linux 命令整理 —— 用户管理
Linux用户管理以读.写.执行动作为权限,以用户组为单位,限制用户行为.对于文件的的操作,可以限制读.写.执行中的哪一种,也可以限制文件所有者.组用户.组外用户相应的权限. 所以,要建立用户,最好先 ...
- 使用JAVA反射初始化数组(转)
在做JSON解析时,遇到了在不知道数组类型的前期下,需要转化为具体类型数组的问题.可以使用JAVA的反射来做. JSONArray jsonArray = (JSONArray) entry.getV ...