因式分解 · 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 ...
随机推荐
- MySQL 5.7忘记密码
关闭正在运行的 MySQL : 1 [root@www.woai.it ~]# service mysql stop 运行 1 [root@www.woai.it ~]# mysqld_safe -- ...
- PyalgoTrade 打印收盘价(二)
让我们从一个简单的策略开始,就是在打印收盘价格的过程中: from pyalgotrade import strategy from pyalgotrade.barfeed import yahoof ...
- Java中private、protected、public和default的区别 (转)
本文内容转载自: https://www.cnblogs.com/jingmengxintang/p/5898900.html public: 具有最大的访问权限,可以访问任何一个在classpath ...
- Linux wget auto login and backup database
#!/bin/bash # 这是一份本来打算采用自动备份数据的代码,由于测试过程中出现了无法连接的问题,导致不能测试, # 于是最后放弃了这份代码的进一步的开发,但是记录还是有必要的 login_ur ...
- android logger的使用
1. 进入GitHub页面 https://github.com/orhanobut/logger 2. 在gradle里增加 compile 'com.orhanobut:logger:1.15' ...
- Oracle-SQL高级查询
--一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ---------------- ...
- java程序运行时内存分配详解
java程序运行时内存分配详解 这篇文章主要介绍了java程序运行时内存分配详解 ,需要的朋友可以参考下 一. 基本概念 每运行一个java程序会产生一个java进程,每个java进程可能包含一个 ...
- java 题
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1 ...
- 除去a标签target="_blank"的方法
用Jquery:$(function(){$("div>a").attr("target","_blank");});先查找页面上的d ...
- java web 程序---留言板
思路:一个form表单,用户提交留言 一个页面显示留言内容.用到Vector来存取信息并显示 cas.jsp <body> <form action="fei.jsp&qu ...