Simple Java solution using factors' method


  • 2
    Q
    public class Solution {
    public boolean isUgly(int num) {
        if (num <= 0) return false;
       
        int[] primes = {2, 3, 5};
        for(int prime : primes) {
            while(num % prime == 0) {
                num = num / prime;
            }
            if(num == 1) return true;
        }
        
        return false;
    }
    

    }


Log in to reply
 

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