|
24 | 24 | */
|
25 | 25 | public class _625 {
|
26 | 26 |
|
27 |
| - /**reference: https://discuss.leetcode.com/topic/92854/java-solution-result-array |
28 |
| - * and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted*/ |
29 |
| - public int smallestFactorization(int a) { |
30 |
| - //case 1: a < 10 |
31 |
| - if (a < 10) { |
32 |
| - return a; |
33 |
| - } |
34 |
| - |
35 |
| - //case 2: start with 9 and try every possible digit |
36 |
| - List<Integer> resultArray = new ArrayList<>(); |
37 |
| - for (int i = 9; i > 1; i--) { |
38 |
| - //if current digit divides a, then store all occurences of current digit in res |
39 |
| - while (a % i == 0) { |
40 |
| - a = a / i; |
41 |
| - resultArray.add(i); |
| 27 | + public static class Solution1 { |
| 28 | + /** |
| 29 | + * reference: https://discuss.leetcode.com/topic/92854/java-solution-result-array |
| 30 | + * and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted |
| 31 | + */ |
| 32 | + public int smallestFactorization(int a) { |
| 33 | + //case 1: a < 10 |
| 34 | + if (a < 10) { |
| 35 | + return a; |
42 | 36 | }
|
43 |
| - } |
44 | 37 |
|
45 |
| - //if a could not be broken in form of digits, return 0 |
46 |
| - if (a != 0) { |
47 |
| - return 0; |
48 |
| - } |
| 38 | + //case 2: start with 9 and try every possible digit |
| 39 | + List<Integer> resultArray = new ArrayList<>(); |
| 40 | + for (int i = 9; i > 1; i--) { |
| 41 | + //if current digit divides a, then store all occurences of current digit in res |
| 42 | + while (a % i == 0) { |
| 43 | + a = a / i; |
| 44 | + resultArray.add(i); |
| 45 | + } |
| 46 | + } |
49 | 47 |
|
50 |
| - //get the result from the result array in reverse order |
51 |
| - long result = 0; |
52 |
| - for (int i = resultArray.size() - 1; i >= 0; i--) { |
53 |
| - result = result * 10 + resultArray.get(i); |
54 |
| - if (result > Integer.MAX_VALUE) { |
| 48 | + //if a could not be broken in form of digits, return 0 |
| 49 | + if (a != 0) { |
55 | 50 | return 0;
|
56 | 51 | }
|
| 52 | + |
| 53 | + //get the result from the result array in reverse order |
| 54 | + long result = 0; |
| 55 | + for (int i = resultArray.size() - 1; i >= 0; i--) { |
| 56 | + result = result * 10 + resultArray.get(i); |
| 57 | + if (result > Integer.MAX_VALUE) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + } |
| 61 | + return (int) result; |
57 | 62 | }
|
58 |
| - return (int) result; |
59 | 63 | }
|
60 | 64 |
|
61 | 65 | }
|
0 commit comments