Short math solution for C++ & Python


  • 0
    Z

    C++ solution

    class Solution {
    public:
        int smallestFactorization(int a) {
            if ( a < 10 )
                return a;
            long b = 0;
            for ( int i = 0; i < 11 && a != 1; ++i ) {
                for ( int d = 9; d > 1; --d ) {
                    if ( a % d == 0 ) {
                        b += pow(10, i) * d;
                        a /= d;
                        break;
                    }
                }
            }
            return ( b > INT_MAX || a != 1 ) ? 0 : b;
        }
    };
    

    Python solution

    class Solution(object):
        def smallestFactorization(self, a):
            if a < 10:
                return a
            b = 0
            for i in xrange(11):
                for d in xrange(9, 1, -1):
                    if a % d == 0:
                        b += (10 ** i) * d
                        a /= d
                        break
            return b if b < (1 << 31) and a == 1 else 0
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.