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 ...
随机推荐
- CentOS下nginx简单安装
说明:环境 系统:Centos 6 软件包:nginx-1.2.4 配置系统yum源 #/etc/yum.repos.d/ #rm -rf ./* vi localhost.repos.d [yumy ...
- FPM打包工具
支持的源类型包: dir: 将目录打包成所需要的类型,可以用于源码编译安装的软件包 rpm: 对rpm进行转换 gem: 对rubygem包进行转换 python: 将Python模块打包成相应的类型 ...
- OC- @property @synthesize
@property 1,在@interface中 2,自动生成setter和getter的声明 #import <Foundation/Foundation.h> @interface P ...
- 解决windows系统80端口被占用问题(转)
在windows下部署web应用(80端口),启动时提示bind 80端口失败 检查端口占用: netstat -ano | findstr 0.0.0.0:80 发现System进程 (pid=4) ...
- Reachability(判断网络是否连接)
类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...
- 【转】Flume日志收集
from:http://www.cnblogs.com/oubo/archive/2012/05/25/2517751.html Flume日志收集 一.Flume介绍 Flume是一个分布式.可 ...
- eclipse将引用了第三方jar包的java项目打成jar包
今天用java开发了一个项目,想要打成jar包,并在linux环境下运行.但是运行时引用的第三方jar包却显示classNotFind错误. 于是查了一下解决办法,在此贴出来,方便以后查阅. 用Ecl ...
- 一个Struts2的实例
对Web应用程序而言,需要跨越HTTP协议的两个障碍——无状态和基于文本. 在没有使用struts的时候,你会有一个很真切的体会,就是如何把html页面上的数据提交给后台处理,以什么格式提交? 这是个 ...
- C# web api返回类型设置为json的两种方法
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- IEnumerable 和 IEnumerator
IEnumerable 接口只包含一个抽象的方法 GetEnumerator(),它返回一个可用于循环访问集合的 IEnumerator 对象,IEnumerator 对象是一个集合访问器. 需要给自 ...