My recursive simple Java Code


  • 0
    H
    public boolean isHappy2(int n) {
        if(n == 1) return true;//1 is included in happy number loop
        if(n == 4) return false;//4 is included in unhappy number loop
        
        int sum = 0;
        while(n > 0){
            sum += (int) Math.pow(n%10, 2);
            n /= 10;
        }
        return isHappy(sum);
    }

Log in to reply
 

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