mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
28 lines
365 B
Go
28 lines
365 B
Go
package leetcode
|
|
|
|
func isHappy(n int) bool {
|
|
if n == 0 {
|
|
return false
|
|
}
|
|
res := 0
|
|
num := n
|
|
record := map[int]int{}
|
|
for {
|
|
for num != 0 {
|
|
res += (num % 10) * (num % 10)
|
|
num = num / 10
|
|
}
|
|
if _, ok := record[res]; !ok {
|
|
if res == 1 {
|
|
return true
|
|
}
|
|
record[res] = res
|
|
num = res
|
|
res = 0
|
|
continue
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
}
|