Posted on

Description

Submission

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

Submission

class Solution {
    int divide(int n, int divisor) {
        while(n) {
            if(n % divisor == 0) n /= divisor;
            else return n;
        }
        return -1;
    }
public:
    bool isUgly(int n) {
        return 1 == divide(divide(divide(n, 2), 3), 5);
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *