Easy Python solution O(n)

  • 0
    class Solution(object):
        def isHappy(self, n):
            dic = set()
            def digSquareSum(m):
                s = 0
                while m:
                    s += (m%10)**2
                    m = m / 10
                return s
            while True:
                s = digSquareSum(n)
                n = s
                if not s in dic:
                    if s==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.