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 ...
随机推荐
- Effective Objective-C 2.0 — 第二章 对象、消息、运行期 - 第六条:理解“属性”这一概念
开发者通过对象来 存储并传递数据. 在对象之间传递数据并执行任务的过程就叫做“消息传递”. 这两条特性的工作原理? Objective-C运行期环境(Objective-C runtime) ,提供了 ...
- post、get的区别
get的参数会显示在浏览器地址栏中,而 post的参数不会显示在浏览器地址栏中: 使用 post提交的页面在点击[刷新]按钮的时候浏览器一般会提示“是否重新提交”,而 get则不会: 用get的页面可 ...
- html兼容性
IE property:value\9; //for all IE IE6 _property:value; IE7 *property:value; IE8 +property:value; IE ...
- 如何在linux环境下安装yaf
我本机的环境配置 linuxMint17.1 php5.5 nginx1.4.6 下面开始安装 下载最新的yaf包 http://pecl.php.net/package/yaf 我下载的最新版本为2 ...
- [译]JavaScript:将字符串两边的双引号转换成单引号
原文:http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html 代码的不一致性总是让人发狂,如果每位开发者都能 ...
- 使用jasmine来对js进行单元测试
互联网的快速发展,给web开发人员带来了前所未有的挑战.对于前端开发,前端开发er所需要编写的js早已不是那些寥寥几行的视觉效果代码.代码量的大增,多人协同,人员素质悬殊不齐,这都需要一个标准,来对代 ...
- 如何在R中加载”xlsx”包
1.下载安装对应系统位数的JDK包(Java SE Development Kit) 2.完成后,安装rJava包-low-level r to Java Interface install.pack ...
- java 练手 谁是最好的Coder
Problem A 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...
- Hadoop 面试题之storm 3个
Hadoop 面试题之八 355.metaq 消息队列 zookeeper 集群 storm集群(包括 zeromq,jzmq,和 storm 本身)就可以完成对商城推荐系统功能吗?还有其他的中间件? ...
- JS获取/设置iframe内对象元素、文档的几种方法
1.IE专用(通过frames索引形象定位): document.frames[i].document.getElementById('元素的ID'); 2.IE专用(通过iframe名称形象定位): ...