C++, runtime 6ms , easy to understand


  • 0
    B

    class Solution {
    public:
    bool isUgly(int num) {
    if(!(num>0))
    return false;
    if(num == 1)
    return true;
    while(!(num%2))
    num = num/2;
    while(!(num%3))
    num = num/3;
    while(!(num%5))
    num = num/5;
    if(num == 1)
    return true;
    else
    return false;
    }
    };


  • 2
    Q

    class Solution {
    public:
    bool isUgly(int num) {

     if(num<1)return false;
       else if(num==1)return true;    
        else{
            while(num>1){
                if(num%2==0)num/=2;
                else if(num%3==0)num/=3;
                else if(num%5==0)num/=5;
                else return false;
            }
            return true;
        }
    }
    

    };

    nearly the same, but in 3ms.


  • 0
    K

    @qiuyingyue0516 Its fucking good!


Log in to reply
 

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