Skip to content

263 ugly number

263. Ugly Number

题目: https://leetcode.com/problems/ugly-number/

难度: Easy

思路:

因为其prime factors 只包括 2, 3, 5,所以比如如果能被2 整除, n = n / 2, 直到不能被2整除,然后同样对3,5检验,如果最终结果为1,那么就是ugly number,否则不过关

注意一下边界条件,当num为0的时候,return一下False

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

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组