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 function that takes an integer n and return all possible combinations of its factors.
Note:
- You may assume that n is always positive.
- Factors should be greater than 1 and less than n.
Example 1:
Input:1
Output: []
Example 2:
Input:37
Output:[]
Example 3:
Input:12
Output:
[
[2, 6],
[2, 2, 3],
[3, 4]
]
Example 4:
Input:32
Output:
[
[2, 16],
[2, 2, 8],
[2, 2, 2, 4],
[2, 2, 2, 2, 2],
[2, 4, 4],
[4, 8]
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道是backtracing可是动不了笔:扩展就是n % i = 0添加因数就行了,退出条件就是把item添加到结果中
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 递推表达式中的因数n要变成n/i
- 退出条件是n<= 1就肯定要用return退出,是否添加取决于item的size是否大于而不是等于1
[二刷]:
helper里面就是个参数,需要从start开始
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 退出条件是n<= 1就肯定要用return退出,是否添加取决于item的size是否大于而不是等于1
[复杂度]:Time complexity: O(乘以每个点是nlgn) Space complexity: O(递归树是lgn)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (n <= 0) return result;
helper(2, new ArrayList<Integer>(), n, result);
return result;
} public void helper(int start, List<Integer> item, int n, List<List<Integer>> result) {
//add the item to the result
if (n <= 1) {
if (item.size() > 1)
result.add(new ArrayList<Integer>(item));
return;
} //calculate the factors
for (int i = start; i <= n; i++) {
if (n % i == 0) {
item.add(i);
helper(i, item, n / i, result);
item.remove(item.size() - 1);
}
}
}
}
254. Factor Combinations 返回所有因数组合的更多相关文章
- [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] 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 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 ...
- 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 ...
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- 将rgb表示方式转换为hex表示方式-------------将hex表示方式转换为rgb表示方式(这里返回rgb数组组合)
/** * kevin 2021.1.4 * 将rgb表示方式转换为hex表示方式 * @param {string} rgbColor 传过来的hex格式的颜色 * @returns { ...
- [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 ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [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 ...
随机推荐
- Discuz! X3 全新安装图文教程
Discuz! 是腾讯旗下 Comsenz 公司推出的以社区为基础的专业建站平台,帮助网站实现一站式服务.让论坛(BBS).个人空间(SNS).门户(Portal).群组(Group).应用开放平台( ...
- bootstrap 模态框事件
事件 描述 实例 show.bs.modal 在调用 show 方法后触发. $('#identifier').on('show.bs.modal', function () { // 执行一些动作. ...
- Linux内核原理第八次作业
Linux内核如何装载和启动一个可执行程序 一.ELF可执行文件格式 ELF格式分类: 可重定位文件:用来和其他object文件一起创建可执行文件和共享文件 可执行文件:指出应该从哪里开始执行 共享文 ...
- py-day3-5 python 函数式编程
# 函数式(方程式 y = 2*x+1) def calc(x): return 2*x+1 print('得出的结果:',calc(6)) 得出的结果: 13 # 面向过程 def calc(x): ...
- FPGA 中三角函数的实现
FPGA 中三角函数的实现
- [蓝桥杯]PREV-10.历届试题_幸运数
问题描述 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的“筛法”生成 . 首先从1开始写出自然数1,,,,,,.... 就是第一个幸运数. 我们从2这个数开始.把所有序号能被2整除的项删除,变 ...
- Ubuntu 16.04 LTS 常用快捷键
在Linux下Win键就是Super键 启动器 Win(长按) 打开启动器,显示快捷键 Win + Tab 通过启动器切换应用程序 Win + 1到9 与点击启动器上的图标效果一样 Win + Shi ...
- 3076: 神经网络(bfs和拓扑排序)
3076: 神经网络 时间限制: 1 Sec 内存限制: 125 MB提交: 7 解决: 5[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目描述 ...
- Python【每日一问】06
问:简述Python文件打开模式 r. w. a. r+.w+.a+之间的区别 答: 1.只读模式 r 文件存在:只读打开,只能执行读操作 文件不存在:报错 # ######## 只读模式r #### ...
- js中slice方法(转)
1.String.slice(start,end)returns a string containing a slice, or substring, of string. It does not m ...