My AC Python solution, 60ms

  • 0

    For every number n, judge ii:
    1. if n is Happy, return True
    2. if n is not Happy:
    1) if n has appeared, it is a circle, the input n won't be Happy, return False
    2) if n hasn't appeared, make the new computed n to be n and see what happened.

    class Solution:
    # @param {integer} n
    # @return {boolean}
    def isHappy(self, n):
        ans = []
        temp = n
        while temp != 1:
            temp = self.compute(temp)
            if temp in ans:
                return False
        return True
    def compute(self, n):
        str_n = str(n)
        temp = 0
        for i in str_n:
            temp += int(i) ** 2
        return temp

Log in to reply

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