Ugly number
October 4, 2022
math-and-geometryProblem URL: Ugly number
If the number is negative, then it's not a ugly number. Then we do exactly what the problem statement says, we divide the number by 2, 3 and 5 until we can't divide it any more with these numbers. If the remainder is 1, then it's an ugly number.
class Solution:
def isUgly(self, n: int) -> bool:
if n <= 0:
return False
for i in 2,3,5:
while n % i == 0 and n>0:
n //= i
return n == 1
Time Complexity: O(n)
Space Complexity: O(1)