LeetCode/solutions/263. Ugly Number.md
2019-09-13 23:08:41 +08:00

19 lines
700 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [263. Ugly Number](https://leetcode.com/problems/ugly-number/description/)
# 思路
判断一个整数是否是丑陋的。若一个整数是正数且其质因子仅包括2、3、5, 那么这个数是丑陋的。特例1也是丑陋的
首先若n非正则直接返回false。否则将其分别除以2、3、5直到不能除进若最后的结果是1则返回true否则返回false。
# C++
``` 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;
}
};
```