呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 263 Ugly Number

文档信息

leetcode 263 Ugly Number

解法一

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

note

解法一的代码精简版

def isUgly(self, num):
    """
    :type num: int
    :rtype: bool
    """
    if num <= 0:
        return False

    for x in [2, 3, 5]:
        while num % x == 0:
            num = num / x

    return num == 1

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub