[抄题]:
给出 n = 8
返回 [[2,2,2],[2,4]]
// 8 = 2 x 2 x 2 = 2 x 4
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
[一句话思路]:
类似于全排列permutation, 用helper,忘了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
分为2-sqrt , n两种情况
[一刷]:
- 没有理解DFS的含义:是recursion的一种,每次都会进入特判中进行添加,所以不用再额外写ans.add(item),DFS中的start要反复用,就是start 不是2
- 要除得尽才能添加,提前的判断条件 不能忘了写。而且sqrt本质是Math类的,要写
- 接口 名 = new 具体实现,主函数调用的时候不要写接口,莫名其妙的错误
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
分为2-sqrt , n两种情况
[复杂度]:helper型DFS 忘了 Time complexity: O(分支的深度次方) Space complexity: O(深度*分支)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
List<List<Integer>>,下次list的实现(引用)都要改成用arraylist 比较好用,不要习惯用linkedlist
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
permutation 全排列
[代码风格] :
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } } }