因式分解
https://www.lintcode.com/problem/652/?utm_source=sc-cheatsheet-cyc
输入:8输出: [[2,2,2],[2,4]]解释: 8 = 2 x 2 x 2 = 2 x 4输入:1 输出: []Last updated
https://www.lintcode.com/problem/652/?utm_source=sc-cheatsheet-cyc
输入:8输出: [[2,2,2],[2,4]]解释: 8 = 2 x 2 x 2 = 2 x 4输入:1 输出: []Last updated
```python
from typing import (
List,
)
class Solution:
"""
@param n: An integer
@return: a list of combination
we will sort your return value in output
"""
def get_factors(self, n: int) -> List[List[int]]:
# write your code here
res = []
self.dfs(n,2,[],res)
return res
def dfs(self, n, next_factor, path, res): #此处N不断减少,next_factor是当前status,从当前可行的路径直到sqrt(n)遍历下去。
if len(path): #把最后一个元素加上,空队列不能作为结果
path.append(int(n))
res.append(path[:])
path.pop()
for i in range(next_factor,math.floor(math.sqrt(n)+1)): #这里起点是next_factor, 不是2.
if n%i == 0:
path.append(i)
self.dfs(n/i, i, path, res)
path.pop()
```