ugly number java solution


  • 0
    1
    class Solution {
        public boolean isUgly(int num) {
           if(num<=0)  return false;
          
           int ret=0;
            int count=0;
           while(count<30)                                  //又是巧妙退出无限循环(但是这种方法并不严谨),与202类似
           {
             if(num==1)  return true;
             if(num%2==0) num/=2;
             else if(num%3==0) num/=3;
               else if(num%5==0) num/=5;
               else    num=num;
               count++;
               if(num==0)   return true;
           }
            return false;
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.