# [LeetCode] 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 == 1) return 1;
string res = "";
for (int k = 9; k >= 2; --k) {
while (a % k == 0) {
res = to_string(k) + res;
a /= k;
}
}
if (a > 1) return 0;
long long num = stoll(res);
return num > INT_MAX ? 0 : num;
}
};

class Solution {
public:
int smallestFactorization(int a) {
if (a < 10) return a;
long long res = 0, cnt = 1;
for (int i = 9; i >= 2; --i) {
while (a % i == 0) {
res += cnt * i;
if (res > INT_MAX) return 0;
a /= i;
cnt *= 10;
}
}
return (a == 1) ? res : 0;
}
};

https://discuss.leetcode.com/topic/92920/concise-c-solution-10-lines-3ms

https://discuss.leetcode.com/topic/92998/c-clean-code-7-line-3-solutions/2

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2017-06-25 05:27  Grandyang  阅读(4764)  评论(1编辑  收藏  举报