Easy to understand Python solution. Beat 90% python submissions.


  • 4
    H
    class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num == 0:
            return False
            
        while num%2 == 0:
            num = num/2
        while num%3 == 0:
            num = num/3
        while num%5 == 0:
            num = num/5
        
        if num == 1:
            return True
        else:
            return False

  • 0
    Z

    why not do this at the end?

    return num == 1

  • 0
    D

    Yep, it is a clear solution. The format looks not so pythonic though :)


  • 0
    B

    class Solution(object):
    def isUgly(self, num):
    """
    :type num: int
    :rtype: bool
    """
    if num < 0:
    return False
    if num == 1:
    return True
    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)

        return False
    

    Runtime Error Message:
    Line 12: RuntimeError: maximum recursion depth exceeded
    Last executed input:
    0

    

    


  • 0
    B

    don't you get the Status:Status: Time Limit Exceeded????


Log in to reply
 

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