|
24 | 24 | */
|
25 | 25 | publicclass_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 |
| -publicintsmallestFactorization(inta) { |
30 |
| -//case 1: a < 10 |
31 |
| -if (a <10) { |
32 |
| -returna; |
33 |
| - } |
34 |
| - |
35 |
| -//case 2: start with 9 and try every possible digit |
36 |
| -List<Integer>resultArray =newArrayList<>(); |
37 |
| -for (inti =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 | +publicstaticclassSolution1 { |
| 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 | +publicintsmallestFactorization(inta) { |
| 33 | +//case 1: a < 10 |
| 34 | +if (a <10) { |
| 35 | +returna; |
42 | 36 | }
|
43 |
| - } |
44 | 37 |
|
45 |
| -//if a could not be broken in form of digits, return 0 |
46 |
| -if (a !=0) { |
47 |
| -return0; |
48 |
| - } |
| 38 | +//case 2: start with 9 and try every possible digit |
| 39 | +List<Integer>resultArray =newArrayList<>(); |
| 40 | +for (inti =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 |
| -longresult =0; |
52 |
| -for (inti =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 | return0;
|
56 | 51 | }
|
| 52 | + |
| 53 | +//get the result from the result array in reverse order |
| 54 | +longresult =0; |
| 55 | +for (inti =resultArray.size() -1;i >=0;i--) { |
| 56 | +result =result *10 +resultArray.get(i); |
| 57 | +if (result >Integer.MAX_VALUE) { |
| 58 | +return0; |
| 59 | + } |
| 60 | + } |
| 61 | +return (int)result; |
57 | 62 | }
|
58 |
| -return (int)result; |
59 | 63 | }
|
60 | 64 |
|
61 | 65 | }
|