[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 ...
随机推荐
- Linux Capability探索实验
Linux内核从2.1版本开始,就开始支持Capabilities的安全机制.Capabilities安全机制提出的目的在于实现系统特权操作的更加细粒度的访问控制,使用户能够根据实际的安全需求来控制r ...
- git中出现remote: HTTP Basic: Access denied
git中出现remote: HTTP Basic: Access denied 1.git clone时出现 Username for 'http://******': *** remote: HTT ...
- Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子
Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子 一个同事的台式机,BIOS启动,500GB硬盘,分了四个MBR分区,C盘是激活的主分区,第二个分区50 ...
- 如何配置nginx负载均衡配置(轮询,权重,ip绑定)
集群是为了解决单节点无法服务高并发的情况,在集群中nginx是如何分配将来自客户端的请求 转发给服务器的 负载均衡可以提高网站的吞吐量(接受和响应),减轻单台服务器的压力 负载均衡提供了三种策略:轮询 ...
- server2012 多用户远程桌面
这个服务器是客户提供的,阿里云平台的服务器.版本是windows-server2012,拿过来的时候,只有一个windows系统,啥都没有. 我们公司的数据库开发设计人员,B/S开发人员,APK开发人 ...
- Unity Shader Graph(二)Dissolve Effect
此篇文章记录Dissolve Effect(溶解特效)的制作过程 软件环境 Unity 2018.1.2f1 Packages: Lightweight Render Pipeline 1.1.11 ...
- CentOS7.5下安装Mycat连接MySQL8.0
MyCat详细介绍,请参考https://www.biaodianfu.com/mycat.html 9066管理端口,请参考https://www.cnblogs.com/parryyang/p/5 ...
- jdbc链接数据库的url两种写法
首先看下面两个jdbc的url 一:jdbc.url=jdbc:oracle:thin:@100.2.194.200:1521/abc二:jdbc.url=jdbc:oracle:thin:@100. ...
- Problem B: 取石子
转换成一个数在(0,X + Y)的加减问题 考虑一种使用线段树处理的方法, 维护前缀最大值, 前缀最小值, 前缀和, 然后查询的时候先询问右区间是否会同时碰到上下界, 会的话左区间无用直接递归右区间, ...
- Flask-在Flask中跨请求传递数据资源
利用 Flask的底层Werkzeug是有缓存支持的,不用使用redis等第三方. 原文地址如下: https://blog.csdn.net/yannanxiu/article/details/52 ...