• [leetcode]202. Happy Number


     1 class Solution(object):
     2     def isHappy(self, n):
     3         """
     4         :type n: int
     5         :rtype: bool
     6         """
     7         flag = set()
     8         while True:
     9             n = self.nextnum(n)
    10             if n == 1:
    11                 return True
    12             if n not in flag:
    13                 flag.add(n)
    14             else:
    15                 return False
    16     def nextnum(self,num):
    17         res = 0
    18         while num:
    19             ge = num%10
    20             num /= 10
    21             res += ge**2
    22         return res

    Write an algorithm to determine if a number is "happy".

    A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

    求一个数是不是快乐的

  • 相关阅读:
    SVG的引入历程
    Webstorm的常用快捷键
    TypeScript
    Vue Router
    ISO8601
    html5语义化
    删除已有的 HTML 元素
    with(){}方法
    Jquery学习笔记
    css权值问题
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6503367.html
Copyright © 2020-2023  润新知