因式分解 · 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 ...
随机推荐
- 错误 1 类,结构或接口成员声明中的标记"="无效
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...
- 算法初探:Tensorflow及PAI平台的使用
前言 Tensorflow这个词由来已久,但是对它的理解一直就停留在“听过”的层面.之前做过一个无线图片适配问题智能识别的项目,基于Tensorflow实现了GoogLeNet - Inception ...
- [团队项目]Scrum 项目1.0 (演说视频)
1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 2.SCRUM 流 ...
- python functiontools 模块
一个内置的模块. 作用是实现了更多的功能, 同时形式上显得很简洁. 虽然在使用很方便, 但其中的原理还是很难复杂的. ------------------------------------- ...
- 自动化工具 Python 调 Jmeter 执行参数化 jmx 脚本
烦透了jmeter输入如下鬼命令: Jmeter -n -t {tmpjmxfile} -l {csvfilename} -e -o {htmlreportpath} 尤其是{htmlreportpa ...
- Chrome 的应用功能越来越强大
Chrome 的应用功能越来越强大 升级到 版本 70.0.3538.77 最早的时候是看到 http 显示地址,现在可以在快捷应用中显示扩展了,还可以看到显示的站点. 现在越来越强大了.
- MyEclipse部署项目到Tomcat上,但是classes文件夹下没有编译项目
在MyEclipse中把项目部署到Tomcat上,但是Tomcat下的classes文件夹下没有编译项目解决方法:1-直接在点击菜单栏的Project--clean,对项目进行clean2-查看菜单栏 ...
- 自定义linux命令
方法一.修改/etc/bashrc文件 在文件底部加入 alias zone="cd /usr/local/webserver" 在命令行输入zone,则会直接进入到制定目录 ...
- 关于layoutparam 请铭记。。。。
//rl_pager 是RelativeLayout findViewById(R.id.rl_pager).setLayoutParams(new RelativeLayout.LayoutPara ...
- MySQL锁之二:锁相关的配置参数
锁相关的配置参数: mysql> SHOW VARIABLES LIKE '%timeout%'; +-----------------------------+----------+ | Va ...