[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

知道是backtracing可是动不了笔:扩展就是n % i = 0添加因数就行了,退出条件就是把item添加到结果中

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 递推表达式中的因数n要变成n/i
  2. 退出条件是n<= 1就肯定要用return退出,是否添加取决于item的size是否大于而不是等于1

[二刷]:

helper里面就是个参数,需要从start开始

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 退出条件是n<= 1就肯定要用return退出,是否添加取决于item的size是否大于而不是等于1

[复杂度]:Time complexity: O(乘以每个点是nlgn) Space complexity: O(递归树是lgn)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

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

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 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. 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. Factor Combinations

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

  6. 将rgb表示方式转换为hex表示方式-------------将hex表示方式转换为rgb表示方式(这里返回rgb数组组合)

      /**  * kevin 2021.1.4  * 将rgb表示方式转换为hex表示方式  * @param {string} rgbColor 传过来的hex格式的颜色  * @returns { ...

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

  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. Python数据类型的可变与不可变

    首先,我们需要知道在python中哪些是可变数据类型,哪些是不可变数据类型.可变数据类型:列表list和字典dict:不可变数据类型:整型int.浮点型float.字符串型string和元组tuple ...

  2. js 遍历EL表达式 list对象

    无法直接通过"${topicList}"获取对象,需要通过<c:forEach>获取list里的对象: var topicListArry = new Array(); ...

  3. InnoDB存储引擎文件

    InnoDB存储引擎文件 MySQL数据库包括数据库本身的文件和存储引擎文件.数据库自身的文件由参数文件(my.cnf).错误日志文件.慢查询日志文件.查询日志文件.二进制日志文件.套接字文件.pid ...

  4. Spring Cloud(Dalston.SR5)--Zuul 网关

    我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...

  5. 在Docker中监控Java应用程序的5个方法

    译者注:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.通常情况下,监控的主要目的在于:减少宕机 ...

  6. can't access lexical declaration `a' before initialization

    <script type="text/javascript"> alert(a); let a=2; </script> 以上代码报错:   Referen ...

  7. 大数据的乘法实现——C语言

    1大数据乘法的算法思路: 输入两个字符串,得到结果,例如:123456789*123456789: 思路:1)首先 123456789*1 = 9   18  27  36  45  54  63   ...

  8. python3笔记<一>基础语法

    随着AI人工智能的兴起,网络安全的普及,不论是网络安全工程师还是AI人工智能工程师,都选择了Python.(所以本菜也来开始上手Python) Python作为当下流行的脚本语言,其能力不言而喻,跨平 ...

  9. spring aop 学习1

    1.配置自动扫描的包 <context:component-scan base-package="com.ddf.spring.aop.impl"/> 2.使用spri ...

  10. JAVA 没有重载运算符,那么 String 类型的加法是怎么实现的,以及String类型不可变的原因和好处

    1, JAVA 不具备 C++ 和 C# 一样的重载运算符 来实现类与类之间相互计算 的功能    这其实一定程度上让编程失去了代码的灵活性, 但是个人认为,这在一定程度上减少了代码异常的概率     ...