Skip to content

Commit 1eb35d8

Browse files
committed
Suggestion for solution 202
1 parent c5a9c0f commit 1eb35d8

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
package leetcode
22

3-
func isHappy(n int) bool {
4-
if n == 0 {
5-
return false
3+
func getSquareOfDigits(n int) int {
4+
squareOfDigits := 0
5+
temporary := n
6+
7+
for temporary != 0 {
8+
remainder := temporary % 10
9+
squareOfDigits += remainder * remainder
10+
temporary /= 10
611
}
7-
res := 0
8-
num := n
12+
13+
return squareOfDigits
14+
}
15+
16+
func isHappy(n int) bool {
917
record := map[int]int{}
10-
for {
11-
for num != 0 {
12-
res += (num % 10) * (num % 10)
13-
num = num / 10
14-
}
15-
if _, ok := record[res]; !ok {
16-
if res == 1 {
17-
return true
18+
19+
for n != 1 {
20+
record[n] = n
21+
22+
n = getSquareOfDigits(n)
23+
24+
for _, previous := range record {
25+
if n == previous {
26+
return false
1827
}
19-
record[res] = res
20-
num = res
21-
res = 0
22-
continue
23-
} else {
24-
return false
2528
}
2629
}
30+
31+
return true
2732
}

0 commit comments

Comments
 (0)