2ms Java Solution, seems a bit stupid...haha


  • 0
    Y
    public boolean isHappy(int n) {
        if(n==0) return false;
        int temp=0;
        while(n!=0){
            temp+=(n%10)*(n%10);
            n=n/10;
        }
        if(temp==1||temp==4||temp==16||temp==37||temp==58||temp==89||temp==145||temp==42||temp==20) return temp==1;
        return isHappy(temp);
    }

  • 0
    J

    I think your code is incorrect. 4 and the number it will generated can not finally become 1...


  • 0
    Y

    but the return of this sentence is temp==1. If temp=4, that'll be false.


  • 0
    Y

    maybe the sentence is too long... so you didn't see the whole code. Haha, that's why I said my code seems quite stupid. :)


Log in to reply
 

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