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.
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]
]
解法一:
先求出所有的factor,在用DFS。注意由于答案中的factor都是升序排列,所以在往line里加入数字时先判断一下要加入line的数字是不是>=里面最后一个(如果有的话)。 或者先对factor.sort(),然后每次取得时候从 factors[i:]里面取,这样也可以保证每个解都是递增数列。
class Solution(object):
def getFactors(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
res = []
line = []
factors = []
for i in range(2,int(math.sqrt(n))+1):
if n%i == 0 and i< math.sqrt(n):
factors.append(i)
factors.append(n/i)
elif n == i*i:
factors.append(i)
if not factors:
return []
self.DFS(factors, n, res, line)
return res def DFS(self, nums, target, res, line):
if target == 1:
res.append([x for x in line])
return if target > 1:
for elem in nums:
if target % elem == 0 and (not line or line and elem >= line[-1]):
line.append(elem)
self.DFS(nums, target/elem, res, line)
line.pop()
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 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 因子组合
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
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- [Erlang 0104] 当Erlang遇到Solr
Joe Armstrong的访谈中有一段关于"打开黑盒子"的阐述,给我留下很深的印象:Joe Armstrong在做XWindows开发时没有使用对应的类库,而是在了解XW ...
- SQL Server Integration Services(SSIS) 包配置与部署
SSIS配置此处的配置方式,主要针对到正式服务器上要修改服务器名,和连接服务器等配置注意:1. 包配置在windows2008上生成后,在windows2003上mysql的配置无法使用,总是报错连接 ...
- 使用 Fiddler 上传微信公众账号 自定义菜单
0.你必须有微信公众账号的服务号.成为开发者之后.... 1.得到你的 appid (xxxxxxoooo)和 secret (oooooooxxxxxxx) 2.用这个链接得到你的 access_t ...
- 探索RegisterAllAreas
在MVC中注册Area时,我们一般会在相应的区域下定义一个继承与AreaRegistration的类,代码如下: public class AdminAreaRegistration : AreaRe ...
- 【java开发】封装与继承
2.封装 把属性封起来(私有化private) 提供了一对公有(public)的方法(getter/setter)来对属性进行操作(读取和设置) 这样做以后可以对属性值的有效性进行判断,避免出现不合法 ...
- 在 KVM 上安装 Win7 虚拟机
之前都是在用Linux 虚机,现在有需要用到Win7 虚机,才发现在 KVM 上安装 Win7 的过程远比想象中的复杂.本文就把其过程做个简单总结. 1. 在 Virtual Machine Mana ...
- PhotoShop算法原理解析系列 - 像素化---》碎片。
接着上一篇文章的热度,继续讲讲一些稍微简单的算法吧. 本文来讲讲碎片算法,先贴几个效果图吧: 这是个破坏性的滤镜,拿美女来说事是因为搞图像的人90%是男人,色色的男人. 关于碎 ...
- microstrip patch antenna
微带天线有很多优点,低成本,易于集成,加工简单,缺点有:效率低.功率容易低.高Q值 ,可能存在表面波,极化效果差,弱电扫性能.带宽窄.微带线的金属厚度t远远小于波长(t << λ0 ,自由 ...
- 【Python数据分析】Python模拟登录(一) requests.Session应用
最近由于某些原因,需要用到Python模拟登录网站,但是以前对这块并不了解,而且目标网站的登录方法较为复杂, 所以一下卡在这里了,于是我决定从简单的模拟开始,逐渐深入地研究下这块. 注:本文仅为交流学 ...
- Struts2 Ajax校验
Ajax(Asynchronous javascript and xml):异步刷新技术 技术组成: CSS + xml +JavaScript +DOM Ajax核心对象: XMLHttpRequ ...