Skip to content
LC-0263 Easy LeetCode

263. Ugly Number

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 42% Topics: Math
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(logn) = O(1)
# Space: O(1)

class Solution(object):
    # @param {integer} num
    # @return {boolean}
    def isUgly(self, num):
        if num == 0:
            return False
        for i in [2, 3, 5]:
            while num % i == 0:
                num /= i
        return num == 1

Solution from kamyu104/LeetCode-Solutions · MIT