[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.

 

分析:

说实话,题目根本没有看懂。"prime factors" 这个理解错了。正确的理解是:所有因子都是2或3或5,即这个数是由多个2,3,5相乘组成的

 

代码:

    public boolean isUgly(int num) {
        
        if(num<=0) return false;  
        if(num==1) return true;  
          
        while(num>=2 && num%2==0) num/=2;  
        while(num>=3 && num%3==0) num/=3;  
        while(num>=5 && num%5==0) num/=5;  
          
        return num==1;  
    }

 

posted @ 2015-10-21 10:12  savageclc26  阅读(153)  评论(0编辑  收藏  举报