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. You may assume that n is always positive.
  2. Factors should be greater than 1 and less than n.

Example 1:

Input: 1
Output: []

Example 2:

Input: 37
Output:[]

Example 3:

Input: 12
Output:
[
[2, 6],
[2, 2, 3],
[3, 4]
]

Example 4:

Input: 32
Output:
[
[2, 16],
[2, 2, 8],
[2, 2, 2, 4],
[2, 2, 2, 2, 2],
[2, 4, 4],
[4, 8]
]

题意:

给定正整数n,返回所有乘积等于n的不同组合(除了n = n)。为避免重复,所有乘数单调递增。

思路:

backtracking

代码:

 class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
helper(res, new ArrayList<>(), n, 2);
return res;
} public void helper(List<List<Integer>> res, List<Integer> list, int n, int start){
if(n == 1){
if(list.size() > 1){
res.add(new ArrayList<>(list));
return;
}
}
for(int i = start; i< = n; i++){
if(n % i== 0){
list.add(i);
helper(res, list, n/i, i);
list.remove(list.size()-1);
}
}
}
}

[leetcode]254. Factor Combinations因式组合的更多相关文章

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

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

  3. 254. Factor Combinations 返回所有因数组合

    [抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...

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

  5. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  6. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  7. [LeetCode] 77. Combinations 全组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 1_2_3_4_5 Html-Css

    --------------------------------------------------------------------------------- HTML骨架 <HTML> ...

  2. PythonStudy——格式化输入小练习

    # 练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式# ------------ info of Egon -----------# Name : Egon# Age : 22# Sex : ...

  3. fromkeys()

    #fromkeys() #说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值 #案例 d=[1,2,3] dict={} dict=dict.fromkeys( ...

  4. DBLinq (MySQL exactly) Linq To MySql(转)

    Linq to SQL很好用,可惜只支持Microsoft SQL Server 和Microsoft SQL Server Compact Edition,目前比较成熟的免费解决方法是DBLinq( ...

  5. Linux打开文件设置

    在某些情况下会要求增加Linux的文件打开数,以增加服务器到处理效率,在Linux下查看最多能打开的文件数量为: cat /proc/sys/fs/file-max 然后设置ulimit -n 文件数 ...

  6. android 监控应用进程

    在android系统中,怎么监控应用的进程改变及消亡呢? 至于监控应用进程能做什么,这个就不多说了,你懂的. 在android系统中有这么一个类ActivityManagerNative,看名称就大概 ...

  7. 猴子选大王的c#实现

    原文地址:猴子选大王的c#实现作者:余文 今天被问到了猴子选大王的意思,题目大意就是说有n只猴子围坐成一个圈,按顺时针方向从1到n编号.然后从1号猴子开始沿顺时针方向从1开始报数,报到m的猴子出局,再 ...

  8. Python循环语句之break与continue的用法

    摘自原文章: http://www.jb51.net/article/73383.htm Python break 语句Python break语句,就像在C语言中,打破了最小封闭for或while循 ...

  9. 微信小程序精品demo

    http://www.jianshu.com/p/0ecf5aba79e1 感谢笔者的分享!

  10. rabbitmq (一)用法

    首先,主机一是window系统,虚拟机二 ubuntu, ubuntu部署了rabbitmq服务端.默认监听5672端口. 由于rabbitmq内部有严格的权限系统,使用之前必须配置好权限. 默认网页 ...