263 丑数
leetcode
题目说丑数满足两个性质:
- 正整数。题目给出的数据范围为\(-2^{31} <= n <= 2^{31} - 1\),所以
n <= 0判断为false - 质因子只有2,3,5 => \(x = 2^x * 3^y * 5^z\),x除以\(2^x\)和\(3^y\)和\(5^z\)后为1且余数为0
class Solution {
public:
bool isUgly(int n) {
if(n <= 0)
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;
}
};