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 ...
随机推荐
- Oracle的导入和导出
导出命令: EXP 用户名/密码@数据库名 BUFFER=64000 file=G:\dmp\full1.dmp OWNER=用户名 导入命令: IMP 用户名/密码@数据库名 BUFFER=64 ...
- JavaStudy——Java之自动拆箱与自动装箱
java基本类型介绍 java中,基本数据类型一共有8种,详细信息如下表: 类型 大小 范围 默认值 byte 8 -128 - 127 0 short 16 -32768 - 32768 0 int ...
- C++对象的构造、析构与拷贝构造
今天下午在研究虚函数的时候遇到了一个问题,觉得很有意思,记录一下. 先看代码: class Base { public: Base(int value) { m_nValue = value; cou ...
- centos7忘记密码处理办法
centos7忘记密码处理办法 此界面按e进入grub编辑界面 进入grub编辑界面.把linux16这行的ro修改为rw init=/sysroot/bin/sh. 按ctrl+x进入单用户模式 登 ...
- Python 回调函数
什么是回调函数? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数: 这是官方的解释,理解上有点费 ...
- MySQL内存使用查看方式
使用版本:MySQL 5.7 官方文档 在performance_schema有如下表记录内存使用情况 mysql> show tables like '%memory%summary%'; + ...
- 6.ST LINK 下调试异常
☆1.无法进入main函数(printf的影响)***为什么有时候可以进入main函数,有什么进入不了main函数? <1> 因为C语言默认使用显示器作为标准输出的设备,所以如果想利 ...
- es的返回数据结构
ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...
- tips:Jquery的attr和prop的区别
Jquery的attr和prop的区别 描述:想做一个复选框checkbox全选的功能,当勾选全选后,将子项的复选框状态设置成一致的, 但遇到了一个问题,就是attr函数并不能改变子项的checkbo ...
- Python 爬虫 Vimeo视频下载链接
python vimeo_d.py https://vimeo.com/228013581 在https://vimeo.com/上看到稀罕的视频 按照上面加上视频的观看地址运行即可获得视频下载链接 ...