[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 ...
随机推荐
- Dynamics 365 CRM 添加自定义按钮
在添加自定义按钮之前,我们需要下载这个工具 RibbonWorkbench, 它是专门针对自定义命令栏和Ribbon区域. 下载之后是一个zip压缩包. 怎样安装RibbonWorkbench: Se ...
- windows下matplotlib编译安装备忘
windows下,codeblocks,mingw安装matplotlib. python下一些源码的编译安装,备忘. matplotlib官网编译好的版本只支持到3.3.我不慎刚下了python3. ...
- MySql开启远程账户登陆总结
1.更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"127.0.0.1"改成"% ...
- 围绕Buganizer的产品流程
做技术的一定知道缺陷跟踪系统(bug系统),更不用说做测试的了,不过普遍都认为这系统是用来记录bug的,其实在google内部,这套系统是产品/项目围绕的核心.Google Buganizer扩展了类 ...
- Android之微信布局篇
一.准备工作: 1. 下载好相关的图片: 2.创建一个名WeiChat的项目,将图片复制到res----->drawable-hdpi目录下. 二.编写代码: 1. 最终效果: 2.微信可划分为 ...
- android AES 加密解密
import java.security.Provider; import java.security.SecureRandom; import javax.crypto.Cipher; import ...
- Kafka命令操作
本文主要介绍Kafka的shell命令: 查看当前服务器所有的topic [hadoop@datanode1 kafka]$ bin/kafka-topics.sh --zookeeper datan ...
- java.util.ConcurrentModificationException详解
引用于http://blog.csdn.net/dabing69221/article/details/40065071 在使用set/map时,一个可爱的小bug:Java.util.Concurr ...
- day29单例模式的4种实现模式
单例模式的四种实现模式单例模式实现方式一: import settings class MySQL: __instance=None def __init__(self, ip, port): ...
- python-web自动化-三种等待方式
当有元素定位不到时,比如下拉框,弹出框等各种定位不到时:一般是两种问题:1 .有frame :2.没有加等待 下面学习三种等待方式: 1.强制等待 sleep(xx)这种方法简单粗暴,不管浏览器是否加 ...