Javascript solution, 5 lines. Clean & short


  • 0
    E
    var isUgly = function(num) {
        if(num<=0) return false; 
        while(parseInt(num/2)===num/2) { num/=2; }  // using the fact that 
        while(parseInt(num/3)===num/3) { num/=3; }  // multiplication is commutative, 
        while(parseInt(num/5)===num/5) { num/=5; }  // hence the order doesn't matter
        return num===1;
    };

Log in to reply
 

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