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 ...
随机推荐
- 使用Alcatraz来管理Xcode插件(转)
转自唐巧的博客:http://blog.devtang.com/blog/2014/03/05/use-alcatraz-to-manage-xcode-plugins/ 简介 Alcatraz是一个 ...
- PhpStorm 快捷键
不容易记住的: Ctrl + Shift + F 查找文本,在项目目录或指定的目录 Ctrl + Shift + R 查找文本并替换,在项目目录或指定的目录 Ctrl + E 打开最近关闭的 ...
- PHP访问MSSQL数据库(实例代码)
本例子只作为简单的引导,实现一个简单的查询: <!DOCTYPE HTML> <html lang="en-US"> <head> <t ...
- axure新手入门教程
首先做个声明:此次教程里为了快速完成,借用了一些网上已有教程的图文,不是剽窃,只图方便.另外,因为汉化版本可能功能名称等略有差别,请自行理解. 名词解释: 线框图:一般就是指产品原型,比如:把线框图尽 ...
- Java中符号位扩展
第一个例子: byte b=-100;b在内存中是以补码的形式存贮的:1001 1100 如果执行char c=(char)b;如3楼企鹅先生所说:b要先变为int,这时增加的位全要用b的符号位填充( ...
- SQL Server2008窗口计算
(一) 窗口的定义:指为用户指定的一组行,也称着"分区".如下图所示的窗口分区.每一个班级看作是一个数据窗口,一共有三个窗口 (二)窗口计算的相关方法 1)over()用法 格式 ...
- 《Effective C++》第三版笔记
阅读此笔记前,请先阅读 <Effective C++>第二版笔记 和 <More Effective C++>笔记 这里只记录与上面笔记不同的条款,主要是 "面对 ...
- 【转】CSS3动画帧数科学计算法
本文来源于:财付通TID 原作者:bboy90 总结都浓缩在这个工具里了,想知道工具的地址或想窥探工具诞生的趣事请往下看 . —————————————————————– 华丽丽的开篇 ...
- JS keycode 事件响应
<script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...
- [AngularJS] 常用指令
常用指令 ng-hide指令,用于控制部分HTML元素可见(ng-hide="false")和不可见状态(ng-hide="true"),如下: <div ...