Skip to content

Commit 2e7f134

Browse files
author
Yeqi Tao
authored
feat: add go solution to lc problem: No.0202 (doocs#828)
1 parent bea0734 commit 2e7f134

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
for k := range tmpMap {
29+
resultMap[k] = false
30+
}
31+
return false
32+
}
33+
tmpMap[n] = true
34+
}
35+
}

0 commit comments

Comments
 (0)