263. Ugly Number
LeetCode 263. Ugly Number
Description
Given an integer n
, return true
if n
is an ugly number.
Ugly number is a positive number whose prime factors only include 2
, 3
, and/or 5
.
Example 1:
Example 2:
Example 3:
Constraints:
-2^31 <= n <= 2^31 - 1
Tags
Math
Solution
If the input number is a non-positive number then return false immediately. We keep dividing this number by 2, 3, 5 until the number is equal to 1 (true) or is not divisible by them (false).
Complexity
Time complexity: , we repeatly divide the given number by 2 or higher;
Space complexity:
Code
Last updated
Was this helpful?