[Locked] 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]
- ]
分析:
因子需要枚举出来,直接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的更多相关文章
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- 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 Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- 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 ...
- [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 ...
- [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 ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- [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 ...
随机推荐
- 使用Convert 类和Parse方法将字符串转换为数值类型
//用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...
- 实现多个ContentProvider对多张表进行操作
http://blog.csdn.net/maylian7700/article/details/7365373 SQLite数据库直接操作类: DatabaseHelper.java package ...
- bash: ./configure: 权限不够 怎么办?
configure没有执行权限 通过chmod给其加上x权限 chmod +x configure 再在该用户下执行 ./configure
- OSX安装nginx和rtmp模块(rtmp直播服务器搭建)
1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- IOS应用程序生命周期&启动周期函数
—程序的生命周期 a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程 b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...
- 【转】 UIView如何管理它的子视图
原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...
- Spring配置多数据源错误总结
由于系统需要调用多个数据源包含mysql,sqlServe和Oracle,所以要在Spring的xml文件中配置多数据源,一下是配置过程中常见的错误: 1.配置的是mysql的数据源,却报oracle ...
- hdoj 1892(二维树状数组)
Problem H Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- C# Winform 双屏显示
双屏显示1 // 利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示. //•获取当前系统连接的屏幕数量: Screen.AllScreens.Count(); //• ...
- Content-Type实体首部字段
现代互联网下,每天都会有数以亿计的各种媒体对象经由HTTP传输,如图像,文本,影视以及软件程序等.这些数据都包含在HTTP报文的实体内容中,如果把HTTP报文想像成一份快递,HTTP实体就是快递实 ...