We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bea0734 commit 2e7f134Copy full SHA for 2e7f134
solution/0200-0299/0202.Happy Number/Solution.go
@@ -0,0 +1,35 @@
1
+var resultMap map[int]bool
2
+
3
+func init() {
4
+ resultMap = make(map[int]bool)
5
+}
6
7
+func calculate(num int) int {
8
+ sum := 0
9
+ for num > 0 {
10
+ k := num % 10
11
+ sum += k * k
12
+ num = num / 10
13
+ }
14
+ return sum
15
16
17
+func isHappy(n int) bool {
18
+ tmpMap := make(map[int]bool)
19
+ for ; ; n = calculate(n) {
20
+ f, ok := resultMap[n]
21
+ if f || n == 1 {
22
+ for k := range tmpMap {
23
+ resultMap[k] = true
24
25
+ return true
26
27
+ if (!f && ok) || tmpMap[n] {
28
29
+ resultMap[k] = false
30
31
+ return false
32
33
+ tmpMap[n] = true
34
35
0 commit comments