Create 263. Ugly Number.md

This commit is contained in:
唐树森 2018-09-30 00:02:49 +08:00 committed by GitHub
parent ee45883d22
commit cde8292958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

18
263. Ugly Number.md Normal file
View File

@ -0,0 +1,18 @@
# [263. Ugly Number](https://leetcode.com/problems/ugly-number/description/)
# 思路
判断一个整数是否是丑陋的。若一个整数是正数且其质因子仅包括2、3、5, 那么这个数是丑陋的。特例1也是丑陋的
首先若n非正则直接返回false。否则将其分别除以2、3、5直到不能除进若最后的结果是1则返回true否则返回false。
# C++
```
class Solution {
public:
bool isUgly(int num) {
if(num <= 0) return false;
while(num % 2 == 0) num /= 2;
while(num % 3 == 0) num /= 3;
while(num % 5 == 0) num /= 5;
if(num == 1) return true;
return false;
}
};
```