Leetcode 263. Ugly Number

Problem

An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

Given an integer n, return true if n is an ugly number.

Algorithm

Just divide the number with 2, 3 and 5.

Code

class Solution:
    def isUgly(self, n: int) -> bool:
        if not n:
            return False

        while n % 2 == 0:
            n //= 2
        while n % 3 == 0:
            n //= 3
        while n % 5 == 0:
            n //= 5
        return n == 1

你可能感兴趣的:(Leetcode,解题报告,入门题,leetcode,算法,职场和发展)