|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
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 |
| - */ |
41 | 6 | public class _254 {
|
42 | 7 |
|
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 | + } |
48 | 14 |
|
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; |
53 | 21 | }
|
54 |
| - return; |
55 |
| - } |
56 | 22 |
|
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 | + } |
62 | 29 | }
|
63 | 30 | }
|
64 | 31 | }
|
65 |
| - |
66 | 32 | }
|
0 commit comments