[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 ...
随机推荐
- @CrossOrigin:解决跨域问题
注解@CrossOrigin解决跨域问题 阅读目录: 一.跨域(CORS)支持: 二.使用方法: 1.controller配置CORS 2.全局CORS配置 3.XML命名空间 4.How does ...
- AD-logon workstation
默认AD登录到限制为64个 原因 发生此问题的原因是User-Workstations属性的Range-Upper值为1,024个字符.使用Active Directory用户和计算机输入NetBIO ...
- GitLab CI runner can't connect to tcp://localhost:2375 in kubernetes
报错的.gitlab-ci.yml配置如下 image: docker:latest services: - docker:dind variables: DOCKER_HOST: tcp://loc ...
- CentOS7.6编译安装redis5.0
yum install gcc wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xvf redis-5.0.0.tar.gz ...
- python OOP
object oriented programming 干啥的 1.避免重名(封装) 2.避免代码重复(继承) 3.将复杂的流程抽象地封装起来 4.模块化程度高,应对复杂编程问题 1)划分职责-要做的 ...
- 【NOIP2015】斗地主 D1 T3 及 增强版 (送命题)
恶心送命模拟题 暴搜顺子,DP预处理剩下的. 由于官方数据太水,很多情况没有讨论的都能过普通版本,想要测试自己代码正确性的同学们可以交交这道题,有很多dalao给出了hack数据 : Luogu P2 ...
- 开源项目(4-2)手势识别-Keras/Theano/OpenCV实现的CNN手势识别
https://github.com/asingh33/CNNGestureRecognizer 我提供了两种捕获模式: 二进制模式:在这里我首先将图像转换为灰度,然后应用高斯模糊效果和自适应阈值滤波 ...
- [ARIA] Create an Accessible Tooltip on a Text Input
Here we use HTML and CSS to create a stylish yet semantic tooltip on a form input. I am using aria-d ...
- Using HAProxy as an API Gateway, Part 3 [Health Checks]
转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...
- 使用terraform 生成自签名证书
terraform 是一个很不错的基础设施工具,我们可以用来做关于基础设施部署的事情,可以实现基础设施即代码 以下演示一个简单的自签名证书的生成(使用tls provider) main.tf 文件 ...