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.
Example: 19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
class Solution(object): def isHappy(self, n): """ :type n: int :rtype: bool """ key = set() s = str(n) key.add(s) re = 0 while True: for i in range(len(s)): re += int(s[i])**2 if re == 1: return True s = str(re) re = 0 if s in key: return False else: key.add(s)
相关推荐
带兔子耳朵发卡的小女孩、彩蛋、Happy Easter、复活节快乐
愚人节活动,愚人节搞笑图片,愚人节来历,快乐愚人节 一起来happy——愚人节ppt模板。
快乐愚人节 一起来happy——愚人节PPT模板.ppt
happy valentine day——简洁情人节PPT模板.ppt
Happy new year——欢庆新年工作总结PPT模板.pptx
happy valentine day,质感爱心,经典灰背景,简洁情人节ppt模板。
LeetCode202_HappyNumber 判断一个数是否为happy nuumber,所谓happy number是指一个数,将其替换为其各位数字的平方和,重复这个过程,如果最终能得到1,这就是happy number;如果陷入一个不包含1的循环中,就不是...
happy表情识别率
2014愚人节ppt模板,WPS出品。Happy April Fools Day——WPS出品愚人节ppt模板。
Happy Chinese New Year——红运当头新年祝福动态贺卡PPT模板.pptx
英语活动——Happy birthday to you.doc
鞭炮爆竹燃放ppt动画,新年祝福动态贺卡,Happy Chinese New Year,红运当头新年祝福动态贺卡ppt模板。
Happy Valentine's Day——情人节创意表白动态贺卡PPT模板.pptx
高兴数的定义是 各个位上数的平方和等于1 比如100,10等
幼儿园教案2021-英语活动——Happy birthday to you.doc
可爱的卡通爱心,可爱的边框线条,爱心图片,适合对情人的表白、对爱人的重温温暖爱的时光贺卡,Happy Valentine's Day,情人节创意表白动态贺卡ppt模板。
话筒、云朵、疑问元素,带着小丑帽子的搞怪表情人物,Happy April Fool's Day,搞怪整蛊的万圣节ppt模板。
Happy new year欢庆新年工作总结计划ppt模板,目录:年度工作概述、工作完成情况、工作存在不足、明年工作计划,矢量卡通人物举杯欢庆2020创意字体封面,彩色几何碎片喜庆红背景流体几何风目录页、过渡页,彩色几何...
happytime rtsp h264 push example.