Solve 202. Happy Number
This commit is contained in:
parent
099bac4380
commit
120ab5f7a0
16
python/202_happy_number.py
Normal file
16
python/202_happy_number.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
class Solution:
|
||||||
|
def isHappy(self, n: int) -> bool:
|
||||||
|
seen = {n}
|
||||||
|
while True:
|
||||||
|
n = self.square_digital_sum(n)
|
||||||
|
if n in seen:
|
||||||
|
return n == 1
|
||||||
|
seen.add(n)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def square_digital_sum(n: int) -> int:
|
||||||
|
total = 0
|
||||||
|
while n > 0:
|
||||||
|
n, digit = divmod(n, 10)
|
||||||
|
total += digit**2
|
||||||
|
return total
|
Loading…
Reference in New Issue
Block a user