C++ Solution


  • 0
    S
    class Solution {
    public:
        int smallestFactorization(int a) {
            if (a < 10) return a;
            int j=0;
            int res[40];
            for (int i=9; i>1; i--) {
                while (a%i == 0) {
                    a = a/i;
                    res[j] = i;
                    j++;
                }
            }
            if (a > 10) { // prime factors > 10
                return 0;
            }
            unsigned long long ans = 0;
            for (int i=j-1; i>=0; i--) {
                ans = ans*10 + res[i];
                if (ans > 2147483647) return 0;
            }
            return ans;
        }
    };
    

  • 0
    N

    you can use a vector instead of the res[40]

    vector<int> res;
    ......
    res.push_back(i)
    

Log in to reply
 

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