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: