因式分解 · 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 ...
随机推荐
- springboot---没有配置数据库启动报错
If you want an embedded database please put a supported one on the classpath. If you have database s ...
- 如何在IDEA启动多个Spring Boot工程实例
在我讲解的案例中,经常一个工程启动多个实例,分别占用不同的端口,有很多读者百思不得其解,在博客上留言,给我发邮件,加我微信询问.所以有必要在博客上记录下,方便读者. step 1 在IDEA上点击Ap ...
- 简单实现MemCachedUtil
package com.chauvet.utils.memcached; import com.chauvet.utils.ConfigUtil; import com.danga.MemCached ...
- RabbitMQ引入
引入MQ话题 可能很多人有疑惑:MQ到底是什么?哪些场景下要使用MQ? 前段时间安装了RabbitMQ,现在就记录下自己的学习心得吧.首先看段程序: class Program { static vo ...
- 设置Nginx日志
在nginx.conf文件或vhosts/*.conf文件中的access_log日志中指定级别为main. http { include mime.types; default_type appli ...
- PAT 1012 数字分类 C语言
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: ...
- VsCode中运行nodeJs代码的简单方法
VsCode安装包默认内置的node debug插件需要配置工程调试运行文件才能正常运行,对于想要运行一个简单的js文件或者就是一段js代码时比较麻烦,为此可以安装Code Runner插件 安装完后 ...
- Spring 部署Tomcat 404 错误解决方案
将Spring项目部署到tomcat后,访问网页出现404错误 HTTP Status 404 – Not Found The origin server did not find a current ...
- Web service 框架比较CXF xfire aisx2 aisx
Web 服务框架.它还体现了从 Axis 1.x 系列获得的经验和最近两年在 Web 服务领域的发展.推出 Axis2 的主要原因之一是从速度和内存方面获得更好的性能——不过还添加了一些新特性和功能. ...
- vim自定义配置之代码折叠
vimConfig/plugin/codeFold-setting.vim "--fold setting-- set foldmethod=syntax " 用语法高亮来定义折叠 ...