JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

Given a positive integer num, return the smallest positive integer x whose multiplication of each digit equals num. If there is no answer or the answer is not fit in 32-bit signed integer, return 0.

Example 1:

Input: num = 48
Output: 68

Example 2:

Input: num = 15
Output: 35

Constraints:

  • $1 <= num <= 2^{31} - 1$

Code

class Solution {
    public int smallestFactorization(int num) {
        if (num == 1) return 1;

        double res = 0;
        int currDigit = 0;

        for (int i = 9; i >= 2; i--) {
            while (num % i == 0) {
                num /= i;
                res = Math.pow(10, currDigit) * i + res;
                currDigit++;
            }
        }
        
        // 22 -> 0
        if(num > 9 || res > Integer.MAX_VALUE) return 0;

        return (int)res;
    }
}