본문 바로가기
카테고리 없음

[LeetCode with Python] 263. Ugly Number

by papajohns 2020. 7. 4.

Difficulty : easy

 

Approach : 1) number range check 2) divide with [2, 3, 5] as much as possible

 

class Solution:
    def isUgly(self, num: int) -> bool:
        if num == 1:
            return True
        elif num <= 0:
            return False

        while not num % 5:
            num //= 5
        
        while not num % 3:
            num //= 3
        
        while not num % 2:
            num //= 2

        return num == 1