LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/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]
]
题解:
DFS needs state current remaining number n, current starting factor, current item list and result.
For i from starting factor to n, check if n%i == 0. If yes, we could add i to current item list and dfs to next level.
starting factor could help maintain there is no duplicate.
e.g. n = 6. When it is divided by 3, the next factor needs to start from 3. Thus there is no [3,2] added since [2, 3] is already in the list. Could use such factor to maintain distinct.
Base case 是target变成了1, 并且item的size要大于1. 这里对item 的size 有要求是因为若初始target = 12, 返回结果是不应该包含{12}这个item的.
Time Complexity: Exponential.
Space: log(target).
AC Java:
public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(n<=1){
return res;
}
findFactors(n, 2, new ArrayList<Integer>(), res);
return res;
}
//注意要写好的signature
private void findFactors(int n, int factor, List<Integer> item, List<List<Integer>> res){
//base case 不但要n==1, 还需要item.size() > 1, 否则n = 2, 就会添加一个item {2}.
if(n == 1 && item.size() > 1){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i = factor; i<=n; i++){
if(n%i == 0){
item.add(i);
findFactors(n/i, i, item, res);
item.remove(item.size()-1);
}
}
}
}
LeetCode Factor Combinations的更多相关文章
- [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 ...
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- 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 ...
- [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 a ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [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 ...
随机推荐
- BZOJ4385 : [POI2015]Wilcze doły
求出前缀和$s$,设$f[i]=s[i+d-1]-s[i-1]$. 从左到右枚举的右端点$i$,左端点$j$满足单调性,若$s[i]-s[j-1]-\max(区间内最大的f)\leq p$,则可行. ...
- Leetcode Sqrt(x)
参考Babylonian method (x0 越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...
- jquery自己手写表单验证
<script type="text/javascript" src="../jquery-1.8.3.js"></script> / ...
- 【HDU】1846 Brave Game
http://acm.hdu.edu.cn/showproblem.php?pid=1846 题意:二人博弈,1堆石子每次取1~m个,没有石子可取的输,输出先手胜利还是后手胜利. #include & ...
- 【BZOJ】3834: [Poi2014]Solar Panels
http://www.lydsy.com/JudgeOnline/problem.php?id=3834 题意:求$max\{(i,j)\}, smin<=i<=smax, wmin< ...
- tornado 学习笔记3 安装
3 安装 安装分为两种方式:自动安装和手动安装,推荐采用自动安装,提前是机器联网的情况下. 3.1 自动安装: 命令:pip install tornado 写一段代码测试一下安装是否成功 # -*- ...
- 团队计划backlog
http://www.cnblogs.com/threemonkey/p/5388439.html
- js 图片处理 Jcrop.js API
引入jquery.Jcrop.min.css和jquery.Jcrop.min.js 参数/接口说明 options 参数说明 名称 默认值 说明 allowSelect true 允许新选框 all ...
- Java_CookieUtil
package com.willow.util; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequ ...
- HttpClient_使用httpclient必须知道的参数设置及代码写法、存在的风险
结论: 如果使用httpclient 3.1并发量比较大的项目,最好升级到httpclient4.2.3上,保证并发量大时能抗住.httpclient 4.3.3,目前还有一些bug:还是用4.2.x ...