Ruka
1 min readApr 3, 2020

[LeetCode][python3]Day02. Happy Number (30-Day LeetCoding Challenge)

30 days! Lets go Lets go!
N2I -2020.04.02

  1. My solution
class Solution:
def isHappy(self, n: int) -> bool:
return self.check(n)

def check(self,n):
if n==1 or n==7:
return True
elif len(str(n))==1:
return False
s=0
for i in str(n):
s+=int(i)**2
return self.check(int(s))

Explanation:

Remember to use return self.check(s) instead of self.check(s), or else you will get a return None in the answer.

Ruka
Ruka

Written by Ruka

HI I’m Ruka. Now a SWE in Taiwan. Contact via email: nayzi9999@gmail.com

No responses yet