-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_254.java
More file actions
28 lines (24 loc) · 733 Bytes
/
Copy pathP_254.java
File metadata and controls
28 lines (24 loc) · 733 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
package leetcode.medium;
import java.util.ArrayList;
import java.util.List;
public class P_254 {
public List<List<Integer>> getFactors(int n) {
final List<List<Integer>> res = new ArrayList<>();
dfs(n, 2, res, new ArrayList<>());
return res;
}
private static void dfs(int n, int start, List<List<Integer>> res, List<Integer> tmp) {
for (int j = start; j * j <= n; j++) {
if (n % j == 0) {
tmp.add(j);
dfs(n / j, j, res, tmp);
tmp.remove(tmp.size() - 1);
}
}
tmp.add(n);
if (tmp.size() > 1) {
res.add(new ArrayList<>(tmp));
}
tmp.remove(tmp.size() - 1);
}
}