LEETCODE 202 HAPPY NUMBER (PYTHON)

79 Views Asked by At
class Solution:
    def isHappy(self, n: int) -> bool:
        visit = set()
        def happy(n):
            temp = n
            n = 0
            for i in str(temp):
                n += int(i)**2
            if n == 1:
                return True
            elif n in visit:
                return False
            else: 
                visit.add(n)
                happy(n)

        return happy(n)

hello guys.

input is 19

n will be 100 > 82 > 68 > 1

but result is False

can somebody explain why?

1

There are 1 best solutions below

3
RapidIce On

End of the happy() function:

else:
    visit.add(n)
    return happy(n)

You're discarding the result of calling happy(n), you need to return that value.