首页 > 代码库 > LeetCode Minimum Factorization

LeetCode Minimum Factorization

625. Minimum Factorization

Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.

If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.

Example 1
Input:

48
Output:
68
Example 2
Input:

15
Output:
35

 

 1 public class Solution {
 2     int[] aa = new int[100];
 3     int j = 0;
 4     boolean flag = true;
 5     public int smallestFactorization(int a) {
 6         smallest(a);
 7         Arrays.sort(aa);
 8         String string = "";
 9         if(j > 31 || !flag) return 0;
10         for(int i = 0; i < 100; i++) {
11             if(aa[i] != 0) {
12                 string = string + aa[i];
13             }
14         }
15         if(string != "") {
16             Long temp = Long.parseLong(string);
17             if(temp > Integer.MAX_VALUE) {
18                 return 0;
19             } else {
20                 return Integer.parseInt(string);
21             }
22         } else 
23         return 0;
24     }
25     public void smallest(int a) {
26         if(a <= 9) {
27             aa[j++] = a;
28             return ; 
29         }
30         for(int i = 9; i >=2 ; i--) {
31             if(a % i == 0) {
32                 aa[j++] = i;
33                 smallest(a/i);
34                 return;
35             }
36         }
37         flag = false;
38         return ;
39     }
40 }

 

LeetCode Minimum Factorization