forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorCombinations.java
More file actions
28 lines (24 loc) · 737 Bytes
/
FactorCombinations.java
File metadata and controls
28 lines (24 loc) · 737 Bytes
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
import java.util.LinkedList;
import java.util.List;
public class FactorCombinations {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> result = new LinkedList<>();
dfs(n, 2, result, new LinkedList<>());
return result;
}
private void dfs(int n, int cur, List<List<Integer>> result, List<Integer> list) {
if (n <= 1) {
if (list.size() > 1) {
result.add(new LinkedList<>(list));
}
return;
}
for (int i = cur; i <= n; i++) {
if (n % i == 0) {
list.add(i);
dfs(n / i, i, result, list);
list.remove(list.size() - 1);
}
}
}
}