zoukankan      html  css  js  c++  java
  • [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;  
        }
  • 相关阅读:
    uploadify上传文件代码
    事务处理拼接sql语句对数据库的操作.异常回滚
    Scrum【转】
    Redis
    mybatis
    Spring MVC
    IOC的理解(转载)
    spring IOC与AOP
    git
    python基础2
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4897023.html
Copyright © 2011-2022 走看看