Java Solution,simple and easy


  • 0
    D
    public boolean isUgly(int num) {
    	if (num >=1 && num <5)
    		return true;
    	if (num < 1 || num == Integer.MAX_VALUE)
    		return false;
    	float n = (float) num;
    	while (n % 2 == 0) {
    		n /= 2;
    	}
    	if (n == 1)
    		return true;
    	while (n % 3 == 0) {
    		n /= 3;
    	}
    	if (n == 1)
    		return true;
    	while (n % 5 == 0) {
    		n /= 5;
    	}
    	if (n == 1)
    		return true;
    	return false;
    }

  • 0
    J

    The condition in the second line can be * if(num >=1 && num< 7) return true; *


  • 0
    D

    ur right,I miss those cases :(


Log in to reply
 

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