Accepted Java Ugly Number Solution


  • 0
    V

    public class Solution {

    public boolean isUgly(int num) {
    
        if(num <= 0){
            return false;
        }
        if(num == 1){
            return true;
        }
        
        if(num % 2 != 0 && num % 3 != 0 && num % 5 != 0){
            return false;
        }
        
        if(num % 2 == 0){
            return isUgly(num / 2);
        }else if(num % 3 == 0){
            return isUgly(num / 3);
        }else if(num % 5 == 0){
            return isUgly(num / 5);
        }
        
        return false;
    }
    

    }


  • 0
    Z

    public class Solution {
    public boolean isUgly(int num) {
    if (num <= 0) {
    return false;
    }
    if (1 == num) {
    return true;
    }
    while (num/2 == num/2.0) {
    num /= 2;
    }
    while (num/3 == num/3.0) {
    num /= 3;
    }
    while (num/5 == num/5.0) {
    num /= 5;
    }
    if (num >= 7) {
    return false;
    }
    else {
    return true;
    }
    }
    }


Log in to reply
 

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