Factor Combinations
Factor Combinations
Problem:
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.
public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> listAll = new ArrayList<>();
if (n < ) return listAll;
List<Integer> list = factors(n);
helper(n, , , list, listAll, new ArrayList<Integer>());
return listAll;
}
public void helper(int n, long value, int index, List<Integer> list, List<List<Integer>> listAll, List<Integer> temp) {
if (index >= list.size() || value >= n) return;
int curValue = list.get(index);
temp.add(curValue);
value *= curValue;
if (n == value) {
listAll.add(new ArrayList<Integer>(temp));
}
helper(n, value, index, list, listAll, temp);
value /= curValue;
temp.remove(temp.size() - );
helper(n, value, index + , list, listAll, temp);
}
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = ; i <= (n + ) / ; i++) {
if (n % i == ) {
list.add(i);
}
}
return list;
}
}
上面代码其实可以用另下面这种方法,原理一样。
public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
helper(, , n, result, list);
return result;
}
public void helper(int start, int product, int n, List<List<Integer>> result, List<Integer> curr) {
if (start > n || product > n) return;
if (product == n) {
List<Integer> t = new ArrayList<>(curr);
result.add(t);
}
for (int i = start; i <= n / ; i++) {
if(i * product > n) break;
if (n % i == ) {
curr.add(i);
helper(i, i * product, n, result, curr);
curr.remove(curr.size() - );
}
}
}
}
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 ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 免费SSL证书Let’s Encrypt
由于我们公司测试环境使用的这个.自己没有亲手搭建使用,但是知道有这个东西.以后使用的话自己直接搞起. 连接文档:http://www.5icool.org/a/201512/a15271.html ...
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...
- vim配色方案设置(更换vim配色方案)
vim配色后,我的 设定底色为黑色,字体为绿色,然后将文件夹设为洋红,默认的注释换为淡黄:其实有一种简单的方法,就是设定为系统配置好的配色方案:转载文章如下: ---------------- ( ...
- linux下vim如何配置markdown插件
在vim中设置markdown语法高亮是一个不错的选择,但是在google中搜索到的很多都是比较老的设置方式,甚至vim插件下载页面都是一个旧的版本.这里总结下我的修改过程,以帮助像我一样纠结的人. ...
- Windows下设置自动关机命令
选择“开始→运行”:1.输入“at 22:00 Shutdown -s”,这样,到了22点电脑就会出现“系统关机”对话框,默认有30秒钟的倒计时并提示你保存工作.2.输入 “Shutdown.exe ...
- PYTHON学习总结
升级 python 版本的问题 升级 python 一般会建立软连接,使系统默认的python指向高版本的 python,如: mv /usr/bin/python /usr/bin/python2. ...
- Git工作流总结
引用自:https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md 说明: 个人在学习Git工作流的过程中,从原有的 S ...
- 【Junit】The import org.junit.Test conflicts with a type defined in the same file报错
引入Junit后,进行单元测试,莫名其妙报了个这样的错误 The import org.junit.Test conflicts with a type defined in the same fil ...
- 微信公众平台回复链接可以直接访问,但不能是锚文字链接<a>标签
最近在学习微信公众平台开发,由于编辑模式和开发模式不可同时开启,在开发模式下如果访客发送关键字过来暂时无法实现关键词自动回复,客服人员先用链接网址直接回复订阅用户,但请注意不能是文字链接,即<a ...
- hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...