Simple 54ms Python Solution with Thoughts


  • 0
    S

    I started dividing the number out by 5 first since it would reduce the number before dividing out by the smaller 3 and then 2.

    class Solution(object):
        def isUgly(self, num):
    
            if num <= 0:
                return False
    
            while num % 5 == 0:
                num /= 5
            while num % 3 == 0:
                num /= 3
            while num % 2 == 0:
                num /= 2
    
            return num == 1

  • 0
    H

    im not sure this optimizes anything (the ordering of your divisions)


Log in to reply
 

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