因式分解 · Factor Combinations
[抄题]:
给出 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 全排列
[代码风格] :
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的更多相关文章
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- 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 ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- [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 ...
随机推荐
- XMU 1246
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1246 求区间内素数个数,经典问题,区间长度10^6,数的取值最多能到10^12(此题范围稍小) 用 ...
- 2017《Java技术》预备作业02
1.学习使用Git和码云托管代码 参考资料:如何使用Git和码云 安装Git 在码云注册账号,新建项目,名称为Java-CS01(02)XXX, 一班为CS01,二班为CS02,后三位或两位为姓名缩写 ...
- 十四年风雨路 苹果iMac电脑进化论
1998年起,在CEO乔布斯的带领下,苹果先后创造除了“软糖”iMac G3.“台灯”iMac G4和“像框”G5.iMac凭借其漂亮的外形和强大的性能,迅速赢得了消费者们的喜爱,甚至改变了整个人类社 ...
- [UOJ213][UNR #1]争夺圣杯
uoj description 一个长为\(n\)的序列,给定一个参数\(m\),求所有长度为\(m\)的区间的最大值之和. 对于所有的\(m\in[1,n]\)你都需要分别求出答案然后异或起来. \ ...
- Linux 脚本编写
第一个shell脚本编写 #!/bin/bash # 上面中的 #! 是一种约定标记, 它可以告诉系统这个脚本需要什么样的解释器来执行; #定义变量: APP_BASE_PATH="/opt ...
- eclipse中点不出来提示
当在用eclipse或是myeclipse时,可能会遇到不能自动提示,就是当你用到点的时候,后面不会出现相关的提示信息.这时,解决方法如下 : 1.菜单window->Preferences-& ...
- java的static研究
(1)static关键字:可以用于修饰属性.方法和类. 1,属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一的一份静态的成员变量(不能修饰临时变量 2,方法:static修饰的方法叫做静态, ...
- bzo j4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据 结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的 ...
- 面试总结之C/C++
source code https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%2 ...
- 在 Laravel 5 中集成七牛云存储实现云存储功能(非上传)
本扩展包基于https://github.com/qiniu/php-sdk开发,是七牛云储存 Laravel 5 Storage版,通过本扩展包可以在Laravel 5中集成七牛云存储功能. 1.安 ...