mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
27 lines
443 B
Go
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
|
|
}
|