题目:
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.
给定一数n,判断它是不是ugly number
ugly number 是指质因数只有2,3,5的数
这题解法相当简单且直观
class Solution: def isUgly(self, n: int) -> bool: if n==0: #n是0直接回传False return False while n%2==0: n=n/2 while n%3==0: n=n/3 while n%5==0: n=n/5 return n==1
将n不断除以2,3,5直到不能整除
若最后剩下的结果是1,即代表n是ugly number
最后执行时间32ms(faster than 93.71%)
那我们下题见