[抄题]:

给出 n = 8 
返回 [[2,2,2],[2,4]] 
// 8 = 2 x 2 x 2 = 2 x 4

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

类似于全排列permutation, 用helper,忘了

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

[画图]:

分为2-sqrt , n两种情况

[一刷]:

  1. 没有理解DFS的含义:是recursion的一种,每次都会进入特判中进行添加,所以不用再额外写ans.add(item),DFS中的start要反复用,就是start 不是2
  2. 要除得尽才能添加,提前的判断条件 不能忘了写。而且sqrt本质是Math类的,要写
  3. 接口 名 = new 具体实现,主函数调用的时候不要写接口,莫名其妙的错误

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

分为2-sqrt , n两种情况

[复杂度]:helper型DFS 忘了 Time complexity: O(分支的深度次方) Space complexity: O(深度*分支)

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

List<List<Integer>>,下次list的实现(引用)都要改成用arraylist 比较好用,不要习惯用linkedlist

[其他解法]:

[Follow Up]:

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

permutation 全排列

[代码风格] :

public class Solution {
/**
* @param n: An integer
* @return: a list of combination
*/
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> ans = new ArrayList<>();
helper(ans, new ArrayList<>(), n, 2);
return ans;
} //helper
private void helper(List<List<Integer>> ans, List<Integer> item, int n, int start) {
//corner case
if (n <= 1) {
if (item.size() > 1) {
ans.add(new ArrayList<>(item));
}
return;
}
//add 2-sqrt//no dfs-start
for (int i = start; i <= Math.sqrt(n); ++i) {
if (n % i == 0) {
item.add(i);
helper(ans, item, n / i, i);
item.remove(item.size() - 1);
} }
//add n
if (start <= n) {
item.add(n);
helper(ans, item, 1, n);
item.remove(item.size() - 1);
} }
}

因式分解 · Factor Combinations的更多相关文章

  1. Factor Combinations

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

  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

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

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

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

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

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

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

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

随机推荐

  1. 手动整合实现SSH项目开发02

    在bean包下建立User类和User.hbm.xml文件,实现User类和数据库表User的映射关系,具体User类不多说,User.hbm.xml如下: <?xml version=&quo ...

  2. 2018-2019-1 20165212 《信息安全系统设计基础》第八周学习总结(pwd)

    2018-2019-1 20165212 <信息安全系统设计基础>第八周学习总结 一.知识点总结 1.三种并发方式 构造并发程序的方法有三种: 进程 线程 I/O多路复用 进程:用内核来调 ...

  3. 《DSP using MATLAB》示例Example 9.9

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. Git 学习记录一

    主要来源参考http://www.runoob.com/git/git-install-setup.html Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 m ...

  5. HDU5296 Annoying problem(LCA)

    //#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> # ...

  6. C#.NET通过Socket实现平行主机之间网络通讯(含图片传输的Demo演示)

    在程序设计中,涉及数据存储和数据交换的时候,不管是B/S还是C/S模式,都有这样一个概念:数据库服务器.这要求一台性能和配置都比较好的主机作为服务器,以满足数目众多的客户端进行频繁访问.但是对于一些数 ...

  7. LDAP基本概念

    LDAP(Lightweight Directory Access Protocol)是一种基于计算模型的客户机/服务器X.500目录服务访问协议.LDAP是从X.500目录访问协议的基础上发展过来的 ...

  8. UTF-8中的BOM

    UTF-8中的BOM UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式.字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB B ...

  9. [搬运] [贪心]NOIP2011 观光公交

    推荐这篇题解:http://www.cnblogs.com/Blacko/archive/2013/10/18/3376597.html 只不过这篇题解有一些细节没有说清,但建议自己思考- Codes ...

  10. mysql-1安装和数据库的管理

    1.安装 直接docker安装,客户端使用Navicat Premium. docker run -d --name csjmysql -p 3306:3306 -e MYSQL_ROOT_PASSW ...