My Java solution with do-while


  • 0
    2
        public boolean isUgly(int num) {
            if(num <= 0)
                return false;
            int temp = num;
            do {
                temp = num;
                if(num % 2 == 0)
                    num = num / 2;
                if(num % 3 == 0)
                    num = num / 3;
                if(num % 5 == 0)
                    num = num / 5;    
            } while(num != temp);  //determine whether num is changed.
            if(num ==1) return true;
            return false;
            
        }
    }
    

Log in to reply
 

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