LeetCode 254. Factor Combinations(因式分解)
原题网址:https://leetcode.com/problems/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:
- Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is
[2, 6]
, not[6, 2]
. - You may assume that n is always positive.
- Factors should be greater than 1 and less than n.
Examples:
input: 1
output:
[]
input: 37
output:
[]
input: 12
output:
[[2, 6],[2, 2, 3],[3, 4] ]
input: 32
output:
[[2, 16],[2, 2, 8],[2, 2, 2, 4],[2, 2, 2, 2, 2],[2, 4, 4],[4, 8] ]
思路:深度优先搜索。
public class Solution {private void find(List<Integer> factors, int target, List<List<Integer>> results) {// System.out.printf("factors=%s\n", factors);if (target == 1) {if (!factors.isEmpty()) {List<Integer> result = new ArrayList<>(factors.size());result.addAll(factors);results.add(result);}return;}int min = factors.isEmpty()? 2 : factors.get(factors.size()-1);int max = factors.isEmpty()? target/2 : target;for(int f = min; f <= max; f ++) {if (target % f != 0) continue;factors.add(f);find(factors, target/f, results);factors.remove(factors.size()-1);}}public List<List<Integer>> getFactors(int n) {List<List<Integer>> results = new ArrayList<>();find(new ArrayList<>(), n, results);return results;}
}
优化:关键在于i*i<=n,可以大幅提升性能。
public class Solution {private void find(int from, int n, List<Integer> factors, List<List<Integer>> results) {for(int i=from; i*i<=n; i++) {if (n % i == 0) {factors.add(i);find(i, n/i, factors, results);factors.remove(factors.size()-1);}}if (!factors.isEmpty()) {factors.add(n);results.add(new ArrayList<>(factors));factors.remove(factors.size()-1);}}public List<List<Integer>> getFactors(int n) {List<List<Integer>> results = new ArrayList<>();find(2, n, new ArrayList<>(), results);return results;}
}
由于ArrayList较慢,如果固定分配空间可以进一步提升性能。
public class Solution {private void find(int from, int n, int[] factors, int len, List<List<Integer>> results) {for(int i=from; i*i<=n; i++) {if (n % i == 0) {factors[len] = i;find(i, n/i, factors, len+1, results);}}if (len > 0) {factors[len] = n;Integer[] f = new Integer[len+1];for(int i=0; i<=len; i++) f[i] = factors[i];results.add(Arrays.asList(f));}}public List<List<Integer>> getFactors(int n) {int p = 0;for(int i=1; i<=n; p++, i*=2);List<List<Integer>> results = new ArrayList<>();find(2, n, new int[p], 0, results);return results;}
}
LeetCode 254. Factor Combinations(因式分解)相关推荐
- 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 f ...
- leetcode算法题--Combinations
原题链接:https://leetcode.com/problems/combinations/ class Solution {public:vector<vector<int>& ...
- 【DFS】LeetCode 17. Letter Combinations of a Phone Number
LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...
- 【LeetCode】77. Combinations 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】
LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...
- LeetCode 17. Letter Combinations of a Phone Number--笔试题--C++,Python解法
题目地址:Letter Combinations of a Phone Number - LeetCode Given a string containing digits from 2-9 incl ...
- LeetCode 254. 因子的组合(回溯)*
文章目录 1. 题目 2. 解题 1. 题目 整数可以被看作是其因子的乘积. 例如:8 = 2 x 2 x 2;= 2 x 4. 请实现一个函数,该函数接收一个整数 n 并返回该整数所有的因子组合. ...
- Leetcode 17 - Letter Combinations of a Phone Number
题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...
- Leetcode 254.因子的组合
Time: 20191005 Type: Medium 题目描述 整数可以被看作是其因子的乘积. 例如: 8 = 2 x 2 x 2;= 2 x 4. 请实现一个函数,该函数接收一个整数n并返回该整数 ...
最新文章
- APMServ下Xdebug安装与使用
- java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表
- wireshark过滤规则学习总结
- SAP:2019年会有多么不一样?
- php发送验证码短信,php发送短信验证码
- codeforces 110A-C语言解题报告
- Linux学习:第一天,
- 物联网卡云平台如何分析信息数据
- 查询各分类中最大自增ID
- Day_05初始化GDT和IDT
- mysql存储food_Mysql存储过程
- windows虚拟显示器开发(一)
- 学习光线跟踪一样的自3D表征Ego3RT
- 简约卡通双十一营销策划PPT模版
- 年末去字节跳动面试,居然被面试官问的哑口无言,原因竟然是这个!!!
- echarts 环形图 不同区域背景色自定义
- HTTP编程(Java爬虫-简单爬取网页数据)
- 根据指定字符拆分字符串
- nbu恢复mysql_使用NBU进行oracle异机恢复
- Linux Logrotate日志切割管理