263. Ugly Number¶ Time: $O(\log n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public: bool isUgly(int n) { if (n == 0) return false; for (const int prime : {2, 3, 5}) while (n % prime == 0) n /= prime; return n == 1; } }; 1 2 3 4 5 6 7 8 9 10 11 12class Solution { public boolean isUgly(int n) { if (n == 0) return false; for (final int prime : new int[] {2, 3, 5}) while (n % prime == 0) n /= prime; return n == 1; } } 1 2 3 4 5 6 7 8 9 10class Solution: def isUgly(self, n: int) -> bool: if n == 0: return False for prime in 2, 3, 5: while n % prime == 0: n //= prime return n == 1