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
class Solution {
public:
    int smallestFactorization(int a) {
        if (a < 2) return a;
        string s;
        for (int i = 9; i >= 2; i--) {
            while (a % i == 0) {
                s.insert(s.begin(), ('0' + i));
                a /= i;
            }
        }
        return (a > 1 || s.size() > 10 || stol(s) > INT_MAX) ? 0 : stoi(s);
    }
};

 

posted @ 2017-11-24 09:30  jxr041100  阅读(206)  评论(0编辑  收藏  举报