【Leetcode】python - [263] Ugly Number 個人解法筆記

題目出處

263. Ugly Number

難度

Easy

個人範例程式碼

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

        while(n%2 == 0):
            n = n // 2
        while(n%3 == 0):
            n = n // 3
        while(n%5 == 0):
            n = n // 5

        return (n == 1)

算法說明

沒什麼好說的

最近在練習程式碼本身就可以自解釋的 Coding style,可以嘗試直接閱讀程式碼理解

input handling

注意定義,小於等於 0 都不是,return False
等於 1 時,return True

Boundary conditions

當不能整除時,結束迴圈。

Reference