码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Minimum Factorization

时间:2017-06-20 01:03:45      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:example   log   color   ati   class   inpu   .so   not   osi   

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

标签:example   log   color   ati   class   inpu   .so   not   osi   

原文地址:http://www.cnblogs.com/argan/p/7051263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!