原题网址: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:

  1. Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2].
  2. You may assume that n is always positive.
  3. 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(因式分解)相关推荐

  1. 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 ...

  2. leetcode算法题--Combinations

    原题链接:https://leetcode.com/problems/combinations/ class Solution {public:vector<vector<int>& ...

  3. 【DFS】LeetCode 17. Letter Combinations of a Phone Number

    LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...

  4. 【LeetCode】77. Combinations 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】

    LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...

  6. 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 ...

  7. LeetCode 254. 因子的组合(回溯)*

    文章目录 1. 题目 2. 解题 1. 题目 整数可以被看作是其因子的乘积. 例如:8 = 2 x 2 x 2;= 2 x 4. 请实现一个函数,该函数接收一个整数 n 并返回该整数所有的因子组合. ...

  8. Leetcode 17 - Letter Combinations of a Phone Number

    题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...

  9. Leetcode 254.因子的组合

    Time: 20191005 Type: Medium 题目描述 整数可以被看作是其因子的乘积. 例如: 8 = 2 x 2 x 2;= 2 x 4. 请实现一个函数,该函数接收一个整数n并返回该整数 ...

最新文章

  1. APMServ下Xdebug安装与使用
  2. java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表
  3. wireshark过滤规则学习总结
  4. SAP:2019年会有多么不一样?
  5. php发送验证码短信,php发送短信验证码
  6. codeforces 110A-C语言解题报告
  7. Linux学习:第一天,
  8. 物联网卡云平台如何分析信息数据
  9. 查询各分类中最大自增ID
  10. Day_05初始化GDT和IDT
  11. mysql存储food_Mysql存储过程
  12. windows虚拟显示器开发(一)
  13. 学习光线跟踪一样的自3D表征Ego3RT
  14. 简约卡通双十一营销策划PPT模版
  15. 年末去字节跳动面试,居然被面试官问的哑口无言,原因竟然是这个!!!
  16. echarts 环形图 不同区域背景色自定义
  17. HTTP编程(Java爬虫-简单爬取网页数据)
  18. 根据指定字符拆分字符串
  19. nbu恢复mysql_使用NBU进行oracle异机恢复
  20. Linux Logrotate日志切割管理

热门文章

  1. 数据库SQL注入漏洞
  2. 小米、腾讯齐升空 无人机产业隐患多
  3. 分区重分配(二十二)
  4. UTF-8有BOM和无BOM的区别
  5. C语言----typedef(2019.5.29)
  6. 怎么快速复制文件、转移文件到同一新文件夹中
  7. 随访系统学习笔记之ModelBinder
  8. 数组越界及其避免方法,C语言数组越界详解
  9. word里设置了标题,但生成的目录上没有显示
  10. sw标准件不能配置_sw标准件库调不出来(在使用solidworks2009标准件库 调不出零件)...