[
math
]
Leetcode 0263 Ugly Number
Problem statement
https://leetcode.com/problems/ugly-number/
Solution
Just try to divide by 2
, 3
and 5
and try to reach 1
.
Complexity
It is O(log n)
for time and O(1)
for space
Code
class Solution:
def isUgly(self, n):
if n <= 0: return False
for num in [2, 3, 5]:
while n % num == 0: n//=num
return n == 1