2ms Java and C++ simple solution


  • 0
    Java
    public boolean isUgly(int num) {
            if(num<=0) return false;
            int[] primes = {2,3,5};
            for(int i=0; i<3; ++i)
            {
                if(num%primes[i] == 0) 
                {
                    num /= primes[i--];
                }
            }
            return num==1;
        }
    
    C++
    bool isUgly(int num) {
            if(num<=0) return false;
            int primes[3] = {2,3,5};
            for(int i=0; i<3; ++i)
            {
                if(num%primes[i] == 0) 
                {
                    num /= primes[i--];
                }
            }
            return num==1;
        }
    

Log in to reply
 

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