Files
LeetCode-Go/leetcode/0202.Happy-Number/202. Happy Number.go
2021-06-26 18:20:04 +08:00

27 lines
443 B
Go

package leetcode
func isHappy(n int) bool {
record := map[int]int{}
for n != 1 {
record[n] = n
n = getSquareOfDigits(n)
for _, previous := range record {
if n == previous {
return false
}
}
}
return true
}
func getSquareOfDigits(n int) int {
squareOfDigits := 0
temporary := n
for temporary != 0 {
remainder := temporary % 10
squareOfDigits += remainder * remainder
temporary /= 10
}
return squareOfDigits
}