首页 > 代码库 > [LeetCode] 625. Minimum Factorization

[LeetCode] 625. Minimum Factorization

public class Solution {
    public int smallestFactorization(int a) {
        if (a < 10) {
            return a;
        }
        List<Integer> factor = new ArrayList<>();
        for (int i = 9; i > 1; i--) {
            while (a % i == 0) {
                factor.add(i);
                a = a / i;
            }
        }
        if (a != 1) {
            return 0;
        }
        long result = 0; // otherwise, overflow
        for (int i = factor.size() - 1; i >= 0; i--) {
            result = result * 10 + factor.get(i);
            if (result > Integer.MAX_VALUE) {
                return 0;
            }
        }
        return (int)result;
    }
}

 

[LeetCode] 625. Minimum Factorization