Numbers can be regarded as product of its factors. For example,

  1. 8 = 2 x 2 x 2;
  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:

  1. []

input: 37
output:

  1. []

input: 12
output:

  1. [
  2. [2, 6],
  3. [2, 2, 3],
  4. [3, 4]
  5. ]

input: 32
output:

  1. [
  2. [2, 16],
  3. [2, 2, 8],
  4. [2, 2, 2, 4],
  5. [2, 2, 2, 2, 2],
  6. [2, 4, 4],
  7. [4, 8]
  8. ]

Numbers can be regarded as product of its factors. For example,

  1. 8 = 2 x 2 x 2;
  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:

  1. []

input: 37
output:

  1. []

input: 12
output:

  1. [
  2. [2, 6],
  3. [2, 2, 3],
  4. [3, 4]
  5. ]

input: 32
output:

  1. [
  2. [2, 16],
  3. [2, 2, 8],
  4. [2, 2, 2, 4],
  5. [2, 2, 2, 2, 2],
  6. [2, 4, 4],
  7. [4, 8]
  8. ]

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

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

Java:

  1. public class Solution {
  2. public List<List<Integer>> getFactors(int n) {
  3. List<List<Integer>> result = new ArrayList<List<Integer>>();
  4. helper(result, new ArrayList<Integer>(), n, 2);
  5. return result;
  6. }
  7.  
  8. public void helper(List<List<Integer>> result, List<Integer> item, int n, int start){
  9. if (n <= 1) {
  10. if (item.size() > 1) {
  11. result.add(new ArrayList<Integer>(item));
  12. }
  13. return;
  14. }
  15.  
  16. for (int i = start; i * i <= n; ++i) {
  17. if (n % i == 0) {
  18. item.add(i);
  19. helper(result, item, n/i, i);
  20. item.remove(item.size()-1);
  21. }
  22. }
  23.  
  24. int i = n;
  25. item.add(i);
  26. helper(result, item, 1, i);
  27. item.remove(item.size()-1);
  28. }
  29. }

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

  1. class Solution:
  2. # @param {integer} n
  3. # @return {integer[][]}
  4. def getFactors(self, n):
  5. result = []
  6. factors = []
  7. self.getResult(n, result, factors)
  8. return result
  9.  
  10. def getResult(self, n, result, factors):
  11. i = 2 if not factors else factors[-1]
  12. while i <= n / i:
  13. if n % i == 0:
  14. factors.append(i);
  15. factors.append(n / i);
  16. result.append(list(factors));
  17. factors.pop();
  18. self.getResult(n / i, result, factors);
  19. factors.pop()
  20. i += 1

C++:

  1. // Time: O(nlogn) = logn * n^(1/2) * n^(1/4) * ... * 1
  2. // Space: O(logn)
  3.  
  4. // DFS solution.
  5. class Solution {
  6. public:
  7. vector<vector<int>> getFactors(int n) {
  8. vector<vector<int>> result;
  9. vector<int> factors;
  10. getResult(n, &result, &factors);
  11. return result;
  12. }
  13.  
  14. void getResult(const int n, vector<vector<int>> *result, vector<int> *factors) {
  15. for (int i = factors->empty() ? 2 : factors->back(); i <= n / i; ++i) {
  16. if (n % i == 0) {
  17. factors->emplace_back(i);
  18. factors->emplace_back(n / i);
  19. result->emplace_back(*factors);
  20. factors->pop_back();
  21. getResult(n / i, result, factors);
  22. factors->pop_back();
  23. }
  24. }
  25. }
  26. };

  

类似题目:

[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. @CrossOrigin:解决跨域问题

    注解@CrossOrigin解决跨域问题 阅读目录: 一.跨域(CORS)支持: 二.使用方法: 1.controller配置CORS 2.全局CORS配置 3.XML命名空间 4.How does ...

  2. AD-logon workstation

    默认AD登录到限制为64个 原因 发生此问题的原因是User-Workstations属性的Range-Upper值为1,024个字符.使用Active Directory用户和计算机输入NetBIO ...

  3. GitLab CI runner can't connect to tcp://localhost:2375 in kubernetes

    报错的.gitlab-ci.yml配置如下 image: docker:latest services: - docker:dind variables: DOCKER_HOST: tcp://loc ...

  4. CentOS7.6编译安装redis5.0

    yum install gcc wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xvf redis-5.0.0.tar.gz ...

  5. python OOP

    object oriented programming 干啥的 1.避免重名(封装) 2.避免代码重复(继承) 3.将复杂的流程抽象地封装起来 4.模块化程度高,应对复杂编程问题 1)划分职责-要做的 ...

  6. 【NOIP2015】斗地主 D1 T3 及 增强版 (送命题)

    恶心送命模拟题 暴搜顺子,DP预处理剩下的. 由于官方数据太水,很多情况没有讨论的都能过普通版本,想要测试自己代码正确性的同学们可以交交这道题,有很多dalao给出了hack数据 : Luogu P2 ...

  7. 开源项目(4-2)手势识别-Keras/Theano/OpenCV实现的CNN手势识别

    https://github.com/asingh33/CNNGestureRecognizer 我提供了两种捕获模式: 二进制模式:在这里我首先将图像转换为灰度,然后应用高斯模糊效果和自适应阈值滤波 ...

  8. [ARIA] Create an Accessible Tooltip on a Text Input

    Here we use HTML and CSS to create a stylish yet semantic tooltip on a form input. I am using aria-d ...

  9. Using HAProxy as an API Gateway, Part 3 [Health Checks]

    转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...

  10. 使用terraform 生成自签名证书

    terraform 是一个很不错的基础设施工具,我们可以用来做关于基础设施部署的事情,可以实现基础设施即代码 以下演示一个简单的自签名证书的生成(使用tls provider) main.tf 文件 ...