-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path254Solution.java
29 lines (29 loc) · 974 Bytes
/
254Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 254. Factor Combinations
//Assumption: n is always positive, 1<factors<n
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
getFactorsHelper(result, path, n, 2);
return result;
}
private void getFactorsHelper(List<List<Integer>> result, List<Integer> path, int n, int start) {
//base case
if (n <= 1) {
//not contain self - n
if (path.size() > 1) {
result.add(new ArrayList<>(path));
return;
}
}
// from 2 to n iterate to find factors
for (int i = start; i <= n; i++) {
if (n % i == 0) {
path.add(i);
getFactorsHelper(result, path, n/i, i);
// backtracking: add first then remove
path.remove(path.size() - 1);
}
}
}
}