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 ...
随机推荐
- Noppoo choc mini 84 @XUbuntu13.10 compatibility setting
Months ago, I bought the keyboard Noppoo Choc Mini 84keys for using under XUbuntu12.10, and I have f ...
- flex 监听网络连接情况
NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange); private ...
- WPF 心形线算法
今天在网上查找下心形算法公式,自己便按照公式写下来标记在博客,主要是方便以后查看! private int maxStep = 520; private double radius; private ...
- oracle窗口函数中range interval的使用
oracle窗口函数中range interval配合一般用来针对指定时间范围进行统计.其中range表示范围,between...and 表示之前的范围和之后的范围 , CURRENT ROW表示当 ...
- Query execution was interrupted, max_statement_time exceeded
版本:5.6.16 群里看见一个问题,在备份的时候,报如下错误:[root@B28-19-75 bak]# mysqldump -root -p --single-transaction --mast ...
- 安装mysql 5.5.14 报错
提示cmake nod foundyum install cmake 原因是曾经服务器安装过mysql数据库Installing MySQL system tables...101223 14:28: ...
- boost muti-thread
背景 • 今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时,复杂的处理以线程的形式单独 ...
- 查看Linux磁盘空间大小
一.df 命令: df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 举例: [root@localhost beinan ...
- WPF 多线程处理(6)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...
- Ruby 语法快速入门
作用域:指的是类,模块,方法 常量:无需指定类型,全大写 FANS = 100 puts "We have" + FANS.to_s + "fans" 变量 局 ...