A straightforward solution c++


  • 0
    D

    Try to remove the prime factors of 2, 3, and 5 as many as possible and see if the remaining number is 1

    class Solution {
    public:
        bool isUgly(int num) {
            if(num<=0) return false;
            int factor[] ={2,3,5}, i=0;
            for(i=0; i<3;++i)
                while(num%factor[i] == 0) num/=factor[i];
    
            return num==1;
        }
    };

Log in to reply
 

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