丑数 就是只包含质因数 2
、3
和 5
的正整数。
给你一个整数 n
,请你判断 n
是否为 丑数 。如果是,返回 true
;否则,返回 false
。
class Solution {
public:
bool isUgly(int n) {
if(n == 0 ) return false;
if(n == 1 ) return true;
if(n%2 == 0 ) return isUgly(n/2);
if(n%3 == 0 ) return isUgly(n/3);
if(n%5 == 0 ) return isUgly(n/5);
return false;
}
};
思路:若质因数为2,3,5.则不断除质因数最终结果必定为1.