Leetcode Factor Combinations
来源:互联网 发布:怪物猎人帅哥捏脸数据 编辑:程序博客网 时间:2024/04/30 01:57
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.
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]]
Difficulty: Medium
Easy Backtracking.
public class Solution { public void helper(List<List<Integer>> res, List<Integer> temp, int target, int start){ double mid = Math.sqrt((double)(target)); if(target == 1){ res.add(new ArrayList<Integer>(temp)); return; } else{ temp.add(target); res.add(new ArrayList<Integer>(temp)); temp.remove(temp.size() - 1); } for(int i = start; i <= mid;i++){ if(target%i == 0){ temp.add(i); helper(res, temp, target/i, i); temp.remove(temp.size() - 1); } } } public List<List<Integer>> getFactors(int n) { List<List<Integer>> res = new ArrayList<>(); List<Integer> temp = new ArrayList<Integer>(); double mid = Math.sqrt((double)(n)); for(int i = 2; i <= mid;i++){ if(n%i == 0){ temp.add(i); helper(res, temp,n/i, i); temp.remove(temp.size() - 1); } } return res; }}
0 0
- LeetCode 254. Factor Combinations
- Leetcode Factor Combinations
- LeetCode 254. Factor Combinations
- LeetCode 题解(253) : Factor Combinations
- [leetcode ] Factor Combinations -------------因数组合
- [leetcode] 254. Factor Combinations 解题报告
- LeetCode 254. Factor Combinations(因式分解)
- [Leetcode] 254. Factor Combinations 解题报告
- Factor Combinations
- Factor Combinations
- Factor Combinations
- Factor Combinations
- [LeetCode254]Factor Combinations
- 254. Factor Combinations
- 254 Factor Combinations
- 254. Factor Combinations
- Medium 254题 Factor Combinations
- Leetcode 254. Factor Combinations 找一个数的所有因子(Backtracking)
- 2016.6.10自学笔记(异常、类的练习题)
- 梳理spring篇-Aop机制
- Activity,Service,BroadcastReceiver生命周期
- android 进阶之UI
- HDOJ 1242 DFS
- Leetcode Factor Combinations
- bnuoj 51121 Simple Polynomial Problem(栈+运算符重载+多项式运算)
- ios之property修饰符
- java计算日期相差的天、时、分、秒
- Scrapy项目unicodeDecodeError_ascii错误的解决
- JavaScript中的document.cookie的使用
- 多线程及断点续传
- 详细介绍AIDL 的使用
- 用JavaScript实现贪吃蛇游戏