[leetcode] 263. Ugly Number

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

Solution:

class Solution {
public:
    bool isUgly(int num) {
        if (num<=0) return false;
        
        while(num%2==0 || num%3==0 || num%5==0){
            if (num%2==0)
                num = num/2;
            else if(num%3==0)
                num = num/3;
            else if(num%5==0)
                num = num/5;
        }
        return num ==1;
        
    }
};


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/denny851108/article/details/51565106
文章标签: leetcode easy
个人分类: leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

[leetcode] 263. Ugly Number

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭