Leetcode 263. Ugly Number
題目要求判斷一個數是否是醜數。醜數是指素數因子只有2, 3, 5的數。比如說6, 8就是醜數,但是14就不是醜數,因為14的素數因子有7.
只要給定的數可以被2, 3, 5整除,我們就不斷用給定的數去除以2, 3, 5,直到最後得到1就說明這個數的所有素數因子只有2, 3, 5,是醜數,反之不是醜數。需要特別注意的是,0不是醜數,因為除了2, 3, 5以外的其他素數也可以看作是0的約數。
class Solution {
public:
bool isUgly(int num) {
for(int i=2; i<6 && i<=num; ++i)
{
while(num%i==0)
num/=i;
}
return num==1;
}
};
相關文章
- LeetCode-Super Ugly NumberLeetCode
- [LeetCode] Ugly Number 醜陋數LeetCode
- LeetCode (39) Ugly Number I II (醜數)LeetCode
- Leetcode Number of islandsLeetCode
- LeetCode:Largest NumberLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Strobogrammatic NumberLeetCode
- LeetCode-Largest NumberLeetCode
- LeetCode-Number of IslandsLeetCode
- Leetcode Valid NumberLeetCode
- leetcode Palindrome NumberLeetCode
- Leetcode Single NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 9 Palindrome NumberLeetCode
- LeetCode-Additive NumberLeetCode
- LeetCode-Strobogrammatic Number IILeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- LeetCode-Create Maximum NumberLeetCode
- LeetCode-Number of Islands IILeetCode
- [LeetCode] Excel Sheet Column NumberLeetCodeExcel
- Leetcode-Single NumberLeetCode
- Leetcode-Valid NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- Valid Number leetcode javaLeetCodeJava
- Single Number leetcode javaLeetCodeJava
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- Leetcode 933 Number of Recent CallsLeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode-Number of Digit OneLeetCodeGit
- LeetCode136:Single NumberLeetCode
- LeetCode 200. Number of IslandsLeetCode
- Leetcode- Single Number IILeetCode
- leetcode刷題--Happy NumberLeetCodeAPP
- leetcode刷題--Number of 1 BitsLeetCode
- LeetCode - 9. Palindrome NumberLeetCode
- Single Number II leetcode javaLeetCodeJava