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]
]

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]
]

写一个函数,给定一个整数n,返回所有可能的因子组合。

解法:递归。从2开始遍历到sqrt(n),能被n整除就进下一个递归,当start超过sqrt(n)时,start变成n,进下一个递归。

Java:

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

Python: Time: O(nlogn) Space: O(logn)

class Solution:
# @param {integer} n
# @return {integer[][]}
def getFactors(self, n):
result = []
factors = []
self.getResult(n, result, factors)
return result def getResult(self, n, result, factors):
i = 2 if not factors else factors[-1]
while i <= n / i:
if n % i == 0:
factors.append(i);
factors.append(n / i);
result.append(list(factors));
factors.pop();
self.getResult(n / i, result, factors);
factors.pop()
i += 1

C++:

// Time:  O(nlogn) = logn * n^(1/2) * n^(1/4) * ... * 1
// Space: O(logn) // DFS solution.
class Solution {
public:
vector<vector<int>> getFactors(int n) {
vector<vector<int>> result;
vector<int> factors;
getResult(n, &result, &factors);
return result;
} void getResult(const int n, vector<vector<int>> *result, vector<int> *factors) {
for (int i = factors->empty() ? 2 : factors->back(); i <= n / i; ++i) {
if (n % i == 0) {
factors->emplace_back(i);
factors->emplace_back(n / i);
result->emplace_back(*factors);
factors->pop_back();
getResult(n / i, result, factors);
factors->pop_back();
}
}
}
};

  

类似题目:

[LeetCode] 39. Combination Sum 组合之和

[LeetCode] 40. Combination Sum II 组合之和 II

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

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

 

All LeetCode Questions List 题目汇总

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

  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. 254. Factor Combinations 返回所有因数组合

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

  6. Factor Combinations

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

  7. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  8. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

  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. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...

  2. 手写二叉树-先序构造(泛型)-层序遍历(Java版)

    如题 先序构造 数据类型使用了泛型,在后续的更改中,更换数据类型只需要少许的变更代码 层序遍历 利用Node类的level属性 所有属性的权限全为public ,为了方便先这么写吧,建议还是用priv ...

  3. IntelliJ IDEA 2019.2破解

    IntelliJ IDEA 2019.2破解 我是参考这个激活的,使用的激活码的方式,需要在百度云盘下载压缩包 https://zhile.io/2018/08/25/jetbrains-licens ...

  4. Linux——安装并配置Kafka

    前言 Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动 ...

  5. Java 15周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  6. [Flutter] Flexible the Widget height to available space

    Let's say you set widget height to 200, but to different screen, there might not be enough space for ...

  7. 洛谷 SP338 ROADS - Roads 题解

    思路 dfs(只不过要用邻接表存)邻接表是由表头结点和表结点两部分组成,其中表头结点存储图的各顶点,表结点用单向链表存储表头结点所对应顶点的相邻顶点(也就是表示了图的边).在有向图里表示表头结点指向其 ...

  8. rust cargo 一些方便的三方cargo 子命令扩展

    内容来自cargo 的github wiki,记录下,方便使用 可选的列表 cargo-audit - Audit Cargo.lock for crates with security vulner ...

  9. Balanced Ternary String(贪心+思维)

    题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...

  10. (15)Go错误处理

    1.erro(一般错误) package main import ( "errors" "fmt" ) func div(a, b int) (res int) ...