My recursive Java solution


  • 0
    W
    public boolean isUgly(int num) {
        if(num<=0)return false;
        if(num==1)return true;
        boolean two=false, three=false, five=false;
        if(num%2==0)two=isUgly(num/2);
        if(num%3==0)three=isUgly(num/3);
        if(num%5==0)five=isUgly(num/5);
        return two||three||five;
    }

  • 0

    Very inefficient. For example the single input 2041200000 alone takes over 300 ms on the judge system, about 250000 times as slow as the "normal" solution.


Log in to reply
 

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