@@ -18,7 +18,8 @@ public List<List<Integer>> getFactors(int n) {
18
18
List <List <Integer >> resultList = new ArrayList <List <Integer >>();
19
19
// DFS
20
20
//dfs(resultList, new ArrayList<Integer>(), n, 2);
21
- dfs1 (resultList , new ArrayList <Integer >(), n , 2 );
21
+ //dfs1(resultList, new ArrayList<Integer>(), n, 2);
22
+ dfs2 (resultList , new ArrayList <Integer >(), n , 2 );
22
23
23
24
return resultList ;
24
25
}
@@ -52,7 +53,21 @@ private void dfs1(List<List<Integer>> resultList, List<Integer> list, int n, int
52
53
dfs1 (resultList , newList , n / i , i );
53
54
54
55
newList .add (n / i );
55
- resultList .add (new ArrayList <Integer >(newList ));
56
+ resultList .add (newList );
57
+ }
58
+ }
59
+ }
60
+
61
+ private void dfs2 (List <List <Integer >> resultList , List <Integer > list , int n , int start ) {
62
+ for (int i = start ; i * i < n ; i ++) {
63
+ if (n % i == 0 ) {
64
+ list .add (i );
65
+ dfs2 (resultList , list , n / i , i );
66
+
67
+ list .add (n / i );
68
+ resultList .add (new ArrayList <Integer >(list ));
69
+ list .remove (list .size () - 1 );
70
+ list .remove (list .size () - 1 );
56
71
}
57
72
}
58
73
}
0 commit comments