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]
]

分析:

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

代码:

void dfs(int target, int start, vector<int> item, vector<vector<int> > &fc) {
if(target == ) {
//去除num = num ( * 1)的分解情况
if(item.size() > )
fc.push_back(item);
return;
}
for(int i = start; i <= target; i++) {
if(target % i == ) {
item.push_back(i);
dfs(target / i, i, item, fc);
item.pop_back();
}
}
return;
}
vector<vector<int> > factor(int num) {
vector<int> item;
vector<vector<int> > fc;
dfs(num, , item, fc);
return fc;
}

[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. webUploader上传组件 实际运用小结

    WebUploader组件实际介绍: 官网:http://fex.baidu.com/webuploader/doc/index.html 组件优势及优化总结:http://itindex.net/d ...

  2. android 简单的开机自启

    今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.in ...

  3. CSRF 攻击的应对之道

    转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...

  4. Objective - C 中NSString (字符串)与C中的字符串转换问题

    NSString是一个常用的类,NSString是原生支持unicode C中的字符串 比如char * a = "hello world";  是utf8类型的, char* d ...

  5. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...

  6. 【BZOJ1012】【树状数组求区间最值】最大数maxnumber

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  7. 一些javascript免费中文书籍

    在这里谢谢那些无私的人~~这些内容来自这里:我只把js的链接粘到这里了~ 还有其它技术文档, 实在是太多了, 多的看都看不完!!! Google JavaScript 代码风格指南 Google JS ...

  8. Javascript参数传递中值和引用的一种理解

    值(value)和引用(reference)是各种编程语言老生常谈的话题,js也不例外. 我将剖析一个例子的实际运行过程,跟大家分享我对js参数传递中的值和引用的理解. 参考官网数据类型的两种分类,本 ...

  9. AspNet WebApi: 了解下HttpControllerDispatcher,控制器的创建和执行

    HttpControllerDispatcher作为ASPNET WEB API消息处理管道中重要的部分,负责最后控制器系统的激活,action方法的执行,以及最后的响应生成. HtppControl ...

  10. Django socketio 安装

    如果你还没有安装过 gevent,首先需要安装 libevent, 编译安装 libevent 需要安装 Pyhton 开发库. 在Debain上可以运行如下指令: $ sudo apt-get in ...