LeetCode 0254. Factor Combinations Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0254. Factor Combinations

Description

Numbers can be regarded as the product of their factors.

  • For example, 8 = 2 x 2 x 2 = 2 x 4.

Given an integer n, return all possible combinations of its factors. You may return the answer in any order.

Note that the factors should be in the range [2, n - 1].

 

Example 1:

Input: n = 1
Output: []

Example 2:

Input: n = 12
Output: [[2,6],[3,4],[2,2,3]]

Example 3:

Input: n = 37
Output: []

 

Constraints:

  • 1 <= n <= 107

Solutions

Solution 1

PythonJavaC++Go
class Solution: def getFactors(self, n: int) -> List[List[int]]: def dfs(n, i): if t: ans.append(t + [n]) j = i while j * j <= n: if n % j == 0: t.append(j) dfs(n // j, j) t.pop() j += 1 t = [] ans = [] dfs(n, 2) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !