Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 377 Bytes

202快乐数.md

File metadata and controls

20 lines (19 loc) · 377 Bytes
func isHappy(n int) bool {
    // 使用set来记录出现过的数,一旦有循环出现则不是快乐数
    set := make(map[int]bool)
    for n != 1 && !set[n] {
        set[n] = true
        n = nextN(n)
    }
    return n == 1
}

func nextN(n int) int {
    sum := 0
    for n > 0 {
        sum += (n % 10) * (n % 10)
        n = n / 10
    }
    return sum
}