Skip to content

Commit 1f1e277

Browse files
refactor 254
1 parent bf688fa commit 1f1e277

File tree

1 file changed

+18
-52
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-52
lines changed

src/main/java/com/fishercoder/solutions/_254.java

+18-52
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,30 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* Numbers can be regarded as product of its factors. For example,
8-
9-
8 = 2 x 2 x 2;
10-
= 2 x 4.
11-
Write a function that takes an integer n and return all possible combinations of its factors.
12-
13-
Note:
14-
You may assume that n is always positive.
15-
Factors should be greater than 1 and less than n.
16-
Examples:
17-
input: 1
18-
output:
19-
[]
20-
input: 37
21-
output:
22-
[]
23-
input: 12
24-
output:
25-
[
26-
[2, 6],
27-
[2, 2, 3],
28-
[3, 4]
29-
]
30-
input: 32
31-
output:
32-
[
33-
[2, 16],
34-
[2, 2, 8],
35-
[2, 2, 2, 4],
36-
[2, 2, 2, 2, 2],
37-
[2, 4, 4],
38-
[4, 8]
39-
]
40-
*/
416
public class _254 {
427

43-
public List<List<Integer>> getFactors(int n) {
44-
List<List<Integer>> result = new ArrayList<List<Integer>>();
45-
helper(result, new ArrayList<Integer>(), n, 2);
46-
return result;
47-
}
8+
public static class Solution1 {
9+
public List<List<Integer>> getFactors(int n) {
10+
List<List<Integer>> result = new ArrayList<>();
11+
helper(result, new ArrayList<>(), n, 2);
12+
return result;
13+
}
4814

49-
public void helper(List<List<Integer>> result, List<Integer> item, int n, int start) {
50-
if (n <= 1) {
51-
if (item.size() > 1) {
52-
result.add(new ArrayList<Integer>(item));
15+
public void helper(List<List<Integer>> result, List<Integer> item, int n, int start) {
16+
if (n <= 1) {
17+
if (item.size() > 1) {
18+
result.add(new ArrayList<>(item));
19+
}
20+
return;
5321
}
54-
return;
55-
}
5622

57-
for (int i = start; i <= n; ++i) {
58-
if (n % i == 0) {
59-
item.add(i);
60-
helper(result, item, n / i, i);
61-
item.remove(item.size() - 1);
23+
for (int i = start; i <= n; ++i) {
24+
if (n % i == 0) {
25+
item.add(i);
26+
helper(result, item, n / i, i);
27+
item.remove(item.size() - 1);
28+
}
6229
}
6330
}
6431
}
65-
6632
}

0 commit comments

Comments
 (0)