C++ solution for Ugly Number


  • 0
    L

    My C++ solution: bool isUgly(int num) {

        if(num <= 0) return false;
        int res = abs(num);
        while(res)
        {
            if(res % 2 == 0)
                res /= 2;
            else if(res % 3 == 0)
                res /= 3;
            else if(res % 5 == 0)
                res /= 5;
            else break;
        }
        return res == 1;
    }

Log in to reply
 

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