From 3a16650abe53b59bb9ab99c931993ef1f253d22c Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 6 Mar 2023 01:49:19 -0500 Subject: [PATCH] =?UTF-8?q?Update=200202.=E5=BF=AB=E4=B9=90=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python的另一种写法 - 通过字符串来计算各位平方和 --- problems/0202.快乐数.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 1a7d53a3..2687574f 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -132,6 +132,19 @@ class Solution: else: record.add(n) +# python的另一种写法 - 通过字符串来计算各位平方和 +class Solution: + def isHappy(self, n: int) -> bool: + record = [] + while n not in record: + record.append(n) + newn = 0 + nn = str(n) + for i in nn: + newn+=int(i)**2 + if newn==1: return True + n = newn + return False ``` Go: