Numbers can be regarded as product of its factors. For example,

  1. 8 = 2 x 2 x 2;
  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:

  1. []

input: 37
output:

  1. []

input: 12
output:

  1. [
  2. [2, 6],
  3. [2, 2, 3],
  4. [3, 4]
  5. ]

input: 32
output:

  1. [
  2. [2, 16],
  3. [2, 2, 8],
  4. [2, 2, 2, 4],
  5. [2, 2, 2, 2, 2],
  6. [2, 4, 4],
  7. [4, 8]
  8. ]

分析:

  因子需要枚举出来,直接DFS

代码:

  1. void dfs(int target, int start, vector<int> item, vector<vector<int> > &fc) {
  2. if(target == ) {
  3. //去除num = num ( * 1)的分解情况
  4. if(item.size() > )
  5. fc.push_back(item);
  6. return;
  7. }
  8. for(int i = start; i <= target; i++) {
  9. if(target % i == ) {
  10. item.push_back(i);
  11. dfs(target / i, i, item, fc);
  12. item.pop_back();
  13. }
  14. }
  15. return;
  16. }
  17. vector<vector<int> > factor(int num) {
  18. vector<int> item;
  19. vector<vector<int> > fc;
  20. dfs(num, , item, fc);
  21. return fc;
  22. }

[Locked] Factor combinations的更多相关文章

  1. Factor Combinations

    Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...

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

  3. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

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

  5. [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 ...

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

  7. 254. Factor Combinations 返回所有因数组合

    [抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 使用Convert 类和Parse方法将字符串转换为数值类型

    //用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...

  2. 实现多个ContentProvider对多张表进行操作

    http://blog.csdn.net/maylian7700/article/details/7365373 SQLite数据库直接操作类: DatabaseHelper.java package ...

  3. bash: ./configure: 权限不够 怎么办?

    configure没有执行权限 通过chmod给其加上x权限 chmod +x configure 再在该用户下执行 ./configure

  4. OSX安装nginx和rtmp模块(rtmp直播服务器搭建)

    1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  5. IOS应用程序生命周期&启动周期函数

    —程序的生命周期         a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程         b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...

  6. 【转】 UIView如何管理它的子视图

    原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...

  7. Spring配置多数据源错误总结

    由于系统需要调用多个数据源包含mysql,sqlServe和Oracle,所以要在Spring的xml文件中配置多数据源,一下是配置过程中常见的错误: 1.配置的是mysql的数据源,却报oracle ...

  8. hdoj 1892(二维树状数组)

    Problem H Time Limit : 5000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...

  9. C# Winform 双屏显示

    双屏显示1 // 利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示. //•获取当前系统连接的屏幕数量: Screen.AllScreens.Count(); //• ...

  10. Content-Type实体首部字段

      现代互联网下,每天都会有数以亿计的各种媒体对象经由HTTP传输,如图像,文本,影视以及软件程序等.这些数据都包含在HTTP报文的实体内容中,如果把HTTP报文想像成一份快递,HTTP实体就是快递实 ...