[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:
- You may assume that n is always positive.
- Factors should be greater than 1 and less than n.
Example 1:
Input: 1
Output: []
Example 2:
Input: 37
Output:[]
Example 3:
Input: 12
Output:
[
[2, 6],
[2, 2, 3],
[3, 4]
]
Example 4:
Input: 32
Output:
[
[2, 16],
[2, 2, 8],
[2, 2, 2, 4],
[2, 2, 2, 2, 2],
[2, 4, 4],
[4, 8]
]
题意:
给定正整数n,返回所有乘积等于n的不同组合(除了n = n)。为避免重复,所有乘数单调递增。
思路:
backtracking
代码:
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> res = new ArrayList<>();
helper(res, new ArrayList<>(), n, 2);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> list, int n, int start){
if(n == 1){
if(list.size() > 1){
res.add(new ArrayList<>(list));
return;
}
}
for(int i = start; i< = n; i++){
if(n % i== 0){
list.add(i);
helper(res, list, n/i, i);
list.remove(list.size()-1);
}
}
}
}
[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 ...
- 254. Factor Combinations 返回所有因数组合
[抄题]: Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write ...
- 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] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- docker之 网络模式和跨主机通信
Docker的四种网络模式Bridge模式 当Docker进程启动时,会在主机上创建一个名为docker0... Docker的四种网络模式 Bridge模式 当Docker进程启动时,会在主机上创建 ...
- win10 hyper-v 外网设置
1. hyper-v管理器中添加 [虚拟交换机] 2. 为虚拟机添加网络适配器 在[虚拟交换机]中选择新创建的交换机 3. 在本本的[网络连接]中,按下 CTRL 键,然后选择[WLAN]和[vEt ...
- Hbase 与mapreduce结合
Hbase和mapreduce结合 为什么需要用mapreduce去访问hbase的数据? ——加快分析速度和扩展分析能力 Mapreduce访问hbase数据作分析一定是在离线分析的场景下应用 案例 ...
- Metadata in HTML
[本文内容大部分来自MDN中文版:https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Introduction_to_HTML/The_head_m ...
- Java中用字符串常量赋值和使用new构造String对象的区别
String str1 = "ABC"; String str2 = new String("ABC"); String str1 = “ABC”;可能创建一个 ...
- solr6.4.1搜索引擎(5)使用zookeeper3.4.9分布式集群
本文讲的是如何使用zookeeper将solr分布式部署,也可以理解为tomcat分布式部署. 为什么要使用zookeeper,请参考文章<Solr的SolrCloud与Master-slave ...
- Python模块subprocess
subprocess的常用用法 """ Description: Author:Nod Date: Record: #-------------------------- ...
- studio--常见设置
13.Butterknife插件:zelezny 12.android studio怎么设置打开项目时打开项目列表? 11.stuido 代码背景颜色设置为护眼模式 ======== 13.But ...
- Make a plan, and stand for it!
我发现博主本人善于事前做计划,事后做总结.但是不善于坚持自己的计划.就拿10.1这个假期来讲,放假前看多许多的攻略,计划了很多条的自驾出行的路线,但是好像一条也没坚持,最后选择了一条临时的线路,而且临 ...
- python数据格式化之pprint
python数据格式化之pprint 2017年06月17日 13:56:33 阅读数:2291 简介 pprint模块 提供了打印出任何Python数据结构类和方法. 模块方法: 1.class p ...