Simple java solution with a trick for 2's


  • 1
    D
    public boolean isUgly(int num) {
            if(num <= 0){
                return false;
            }
            //remove 2's.
            num >>>=  Integer.numberOfTrailingZeros(num);
            
            //remove 3's
            while(num % 3 == 0){
                num = num/3;
            }
            //remove 5's
            while(num %5 == 0){
                num = num /5;
            }
            return num == 1;
        }

Log in to reply
 

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