[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 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]
]
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]
]
写一个函数,给定一个整数n,返回所有可能的因子组合。
解法:递归。从2开始遍历到sqrt(n),能被n整除就进下一个递归,当start超过sqrt(n)时,start变成n,进下一个递归。
Java:
public class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
helper(result, new ArrayList<Integer>(), n, 2);
return result;
}
public void helper(List<List<Integer>> result, List<Integer> item, int n, int start){
if (n <= 1) {
if (item.size() > 1) {
result.add(new ArrayList<Integer>(item));
}
return;
}
for (int i = start; i * i <= n; ++i) {
if (n % i == 0) {
item.add(i);
helper(result, item, n/i, i);
item.remove(item.size()-1);
}
}
int i = n;
item.add(i);
helper(result, item, 1, i);
item.remove(item.size()-1);
}
}
Python: Time: O(nlogn) Space: O(logn)
class Solution:
# @param {integer} n
# @return {integer[][]}
def getFactors(self, n):
result = []
factors = []
self.getResult(n, result, factors)
return result def getResult(self, n, result, factors):
i = 2 if not factors else factors[-1]
while i <= n / i:
if n % i == 0:
factors.append(i);
factors.append(n / i);
result.append(list(factors));
factors.pop();
self.getResult(n / i, result, factors);
factors.pop()
i += 1
C++:
// Time: O(nlogn) = logn * n^(1/2) * n^(1/4) * ... * 1
// Space: O(logn) // DFS solution.
class Solution {
public:
vector<vector<int>> getFactors(int n) {
vector<vector<int>> result;
vector<int> factors;
getResult(n, &result, &factors);
return result;
} void getResult(const int n, vector<vector<int>> *result, vector<int> *factors) {
for (int i = factors->empty() ? 2 : factors->back(); i <= n / i; ++i) {
if (n % i == 0) {
factors->emplace_back(i);
factors->emplace_back(n / i);
result->emplace_back(*factors);
factors->pop_back();
getResult(n / i, result, factors);
factors->pop_back();
}
}
}
};
类似题目:
[LeetCode] 39. Combination Sum 组合之和
[LeetCode] 40. Combination Sum II 组合之和 II
[LeetCode] 216. Combination Sum III 组合之和 III
[LeetCode] 377. Combination Sum IV 组合之和 IV
All LeetCode Questions List 题目汇总
[LeetCode] 254. Factor Combinations 因子组合的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- Factor Combinations
Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [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 ...
随机推荐
- [Reprint] Difference Between Job, Work, And Career
https://www.espressoenglish.net/difference-between-job-work-and-career/ A lot of English learners co ...
- Subsequence(HDU3530+单调队列)
题目链接 传送门 题面 题意 找到最长的一个区间,使得这个区间内的最大值减最小值在\([m,k]\)中. 思路 我们用两个单调队列分别维护最大值和最小值,我们记作\(q1\)和\(q2\). 如果\( ...
- linux卸载及安装mysql 5.7以上
删除: 1.rpm -qa|grep -i mysql 查看安装的mysql 2./usr/local/mysql/support-files/mysql.server stop 停止mys ...
- 由Catalan数所引出的
百度一番: 历史 ·1758年,Johann Segner 给出了欧拉问题的递推关系: ·1838年,研究热潮: –GabrielLame给出完整证明和简洁表达式: –EugèneCharlesCat ...
- 项目Alpha冲刺(团队)-第七天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第七天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...
- MSc in Robotics
MSc in RoboticsProgramming Methods for Robotics AssignmentIrene Moulitsas & Peter SherarCranfiel ...
- 如何有效使用Project(1)——编制进度计划、保存基准
1.前言: 软件产品的研发.升级.定制等,一般都是以项目的形式进行,此时项目进度计划以及资源使用情况就变成了项目经理关注的重点.如何让项目计划有效可控,及时暴露问题?如何查看资源的负荷情况,看资源分配 ...
- hibernate实现增删改查
1.需要先创建学生实体: package pers.zhb.domain; public class Student { private int studentno; private String s ...
- 洛谷 题解 P1828 【香甜的黄油 Sweet Butter】
潇洒の开始 第一步:食用头文件和定义变量, 变量干什么用的说的很清楚 #include<iostream> #include<cstdio> #include<cstri ...
- DACL原理.控制文件的访问权限(文件,注册表.目录.等任何带有安全属性的对象.)
目录 一丶简介 1.DACL是什么. 2.如何创建一个自己控制的文件. 3.SDDL是个什么鬼. 二丶 编写SDDL 控制的文件 一丶简介 1.DACL是什么. DACL称为自主访问的控制列表.是应用 ...