Python long solution with 36ms


  • 0
    L

    Curious why this is faster than the classic solution with the most votes. To me it seems they're doing the same thing.

    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 0:
            return False
        if num == 1:
            return True
        if num % 2 != 0 and num % 3 != 0 and num % 5 != 0:
            return False
        if num % 2 == 0:
            return self.isUgly(num // 2)
        if num % 3 == 0:
            return self.isUgly(num // 3)
        if num % 5 == 0:
            return self.isUgly(num // 5)
    

Log in to reply
 

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