From 659595f2b4d09d45d7301b728ac274b1cd52b39a Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Mon, 31 May 2021 22:26:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index c07405ec..396231fe 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -111,6 +111,24 @@ Python: Go: +```go +func isHappy(n int) bool { + m := make(map[int]bool) + for n != 1 && !m[n] { + n, m[n] = getSum(n), true + } + return n == 1 +} + +func getSum(n int) int { + sum := 0 + for n > 0 { + sum += (n % 10) * (n % 10) + n = n / 10 + } + return sum +} +``` javaScript: