LeetCode 263 Ugly Number

LeetCode 263 Ugly Number Problem

Download Code
class Solution(object):
    # def isUgly(self, num):
    #     """
    #     :type num: int
    #     :rtype: bool
    #     """
    #     if num <= 0:
    #         return False
    #     if num <= 6:
    #         return True
    #     while num % 2 == 0:
    #         num //= 2
    #     while num % 3 == 0:
    #         num //= 3
    #     while num % 5 == 0:
    #         num //= 5
    #     if num == 1:
    #         return True
    #     return False
    def isUgly(self, num):
        if num <= 0:
            return False
        divisors = [2, 3, 5]
        for d in divisors:
            while num % d == 0:
                num /= d
        return num == 1

if __name__ == '__main__':
    s = Solution()
    print s.isUgly(-2147483648)
Download Ugly Number.py

List of all Ugly Number problems

Leetcode 263 Ugly Number problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.