原题链接在这里: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:

  1. Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2].
  2. You may assume that n is always positive.
  3. 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);
}
}
}
}

类似Combination Sum.

LeetCode Factor Combinations的更多相关文章

  1. [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 ...

  2. Factor Combinations

    Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Diophantus of Alexandria[HDU1299]

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  2. 【BZOJ】3240: [Noi2013]矩阵游戏

    题意 给出\(n, m(1 \le n, m \le 10^{1000000})\),求\(f(n, m) \ \mod \ 10^9+7\) $$\begin{cases}f(1, 1) = 1 \ ...

  3. Codeforces Round #210 (Div. 2) C. Levko and Array Recovery

    题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...

  4. POJ 1473 There's Treasure Everywhere!

    题目链接 小小的模拟一下. #include <cstdio> #include <cstring> #include <string> #include < ...

  5. Android -- 动画效果收获(1)

    加载选项菜单         MenuInflater inflater = getMenuInflater();    inflater.inflater(R.menu.menu,menu); An ...

  6. Unity5.x版本AssetBundle打包研究

    Unity5的AssetBundle打包机制和以前版本不太一样.简单的说就是,只要给你要打包的资源设置一个AssetBundleName ,Unity自身会对这些设置了名字的资源进行打包,如果一个资源 ...

  7. C程序演示产生僵死进程的过程

    先抄录网上一段对僵死进程的描述: 僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...

  8. asp.net mvc4 HTTP Error 403.14

    asp.net mvc4项目部署到II&上时,出现HTTP Error 403.14 - Forbidden - The Web server is configured to not lis ...

  9. windows一些快捷键

    1.Win + __ 1)Win + L 锁屏 2)Win + E 资源管理器(就是打开硬盘) 3)Win + D 回到桌面 4)Win + Tab 3D方式切换程序窗口 5)Win + R 运行命令 ...

  10. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...