LeetCode Minimum Factorization

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode@ [310] Minimum Height Trees

LeetCode -- Minimum Path Sum

LeetCode 2187. Minimum Time to Complete Trips

[LeetCode]Minimum Path Sum

leetcode 310. Minimum Height Trees 最小高度树(中等)

LeetCode 939. Minimum Area Rectangle