My 3ms Java solution_(:з」∠)_


  • 2
    A
    public boolean isHappy(int n) {
            int result = 0;
            while (n > 9) {
                result += Math.pow(n % 10, 2);
                n = n / 10;
            }
            result += Math.pow(n, 2);
            if (result > 9) {return isHappy(result);}
            if (result == 1) {
                return true;
            } else {
                return false;
            }
    }

  • 0
    S

    This logic will fail for Happy number : 1111111


Log in to reply
 

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