2ms Solution in JAVA


  • 9
    V

    public class Solution {

    public boolean isUgly(int num) {
        if(num == 0) {
            return false;
        }
        while(num % 5 == 0) {
            num /= 5;
        }
        while(num % 3 == 0) {
            num /= 3;
        }
        while(num % 2 == 0) {
            num /= 2;
        }
        if(num == 1) {
            return true;
        }
        return false;
    }
    

    }


  • 0
    L

    the cleanest and simplest solution so far!


  • 0

    why not return num==1; instead of an if statement


  • 0
    T

    i also use this method to solve this problem , but i find that when i input 2147483648 ,my code will be wrong ,and the given answer will be false , how do u think of it.


  • 0
    V

    @tytttta: If you are using Java, the range of int is from -2147483648 to 2147483647. So the error you receive is probably because your inr is out of range because it is greater than 2147483647.

    To avoid confusion, you can refer to the max int value as 'Integer.MAX_VALUE' rather than putting in the value.


  • 0
    T

    u dont understand my words. I know 2147483648 is greater than the max value of int ,But the expeceted answer can give the right answer .I want know how it deal with this number.


  • 0
    V

    Sorry, I couldn't understand your question. Could you tell me the exact input and expected output that you are trying to get.


  • 0
    T

    When the input is 2147483648,the result of my code will be true, and the given answer will be false! But my code(Be same to yours) is accepted! I want to know if it is a bug?


Log in to reply
 

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