Simple Python solution


  • 0
    W
    class Solution(object):
        def isUgly(self, num):
            if num == 0:
                return False
            if num == 1:
                return True
                
            for x in (2, 3, 5):
                if num % x == 0:
                    return self.isUgly(num / x)
            
            return False
    

  • 0
    F

    Hey, that's pretty much my algorithm in Java

    public class Solution {
        public boolean isUgly(int num) {
            if(num <= 0) {
                return false;
            }
            if(num == 1) {
                return true;
            }
            
            int[] uglyNums = new int[]{2, 3, 5};
            
            for(int i = 0; i < 3; i++) {
                if(num % uglyNums[i] == 0) {
                    return isUgly(num/uglyNums[i]);
                }
            }
            
            return false;
        }
    }

Log in to reply
 

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