4ms C++ solution with for each.


  • 1
    R
    if (num == 0) return false;
    if (num == 1) return true;
    
    int factors[] = { 2, 3, 5 };
    for (auto e : factors) {
        while (num % e == 0) { num /= e; }
    }  // foreach in factors
    
    return num == 1;

  • 0

    That's not a solution for Ugly Number II.


Log in to reply
 

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