Short 1ms O(1) space Java solution


  • 0
    R
    public boolean isHappy(int n) {
        for (int sum = 0; n / 10 != 0 || n % 2 != 0 || (n == 1); n = sum, sum = 0) {
            for (int i = n; i > 0; i /= 10)
                sum += Math.pow(i % 10, 2);
            if (sum == 1)
                return true;
        }
        return false;
    }
    

Log in to reply
 

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