Sharing my Java answer


  • 0
    4
    private static boolean isHappyNumber(int num) {
    
            int sum = 0;
            while (num > 0) {
                sum = sum + (num % 10) * (num % 10);
                num = num / 10;
    
                if (num <= 0) {
                    if (sum == 1) return true;
                    else if (sum < 10) return false;
                    else {
                        num = sum;
                        sum = 0;
                    }
                }
            }
    
            return false;
        }

  • 0
    Y

    You answer is incorrect. Try num = 1112. 1112 - > 7 -> 49 -> 97 -> 130 -> 10 -> 1.


Log in to reply
 

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