快乐数
2020-05-05 本文已影响0人
7赢月
题目描述
https://leetcode-cn.com/problems/happy-number/
解
package main
func isHappy(n int) bool {
var (
filter = make(map[int]struct{})
c = 0
)
for {
r := n % 10
c += r * r
n = n / 10
if n != 0 {
continue
}
if c == 1 {
return true
}
if _, ok := filter[c]; ok {
return false
}
filter[c] = struct{}{}
n = c
c = 0
}
}
思路
这个是简单模式的,审题之后使用循环就能很快解题了!