快乐数

作者: 7赢月 | 来源:发表于2020-05-05 15:25 被阅读0次

    题目描述

    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
        }
    }
    

    思路

    这个是简单模式的,审题之后使用循环就能很快解题了!

    相关文章

      网友评论

          本文标题:快乐数

          本文链接:https://www.haomeiwen.com/subject/lqvmghtx.html