202. Happy Number

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

防止死循环, 加一个set存储这个值, 有重复的sum即break。

坑爹货 sum = n%10 * n%10 得到结果竟然不对。 必须要temp = n%10 ; sum = temp*temp;

运算符优先级的问题  sum =( n%10) * (n%10);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,949评论 0 23
  • 不知道怎样开始写下第一遍文字。就从2016年1月19日开始记录陶生活的点点滴滴与起起伏伏吧,或平淡、或无聊、或高兴...
    月光洒落阅读 152评论 1 1
  • 电影《血战钢锯岭》由导演梅尔·布吉森指导。影片改编自二战上等兵军医待斯蒙德·道斯的真实经历。导演采用传统的叙事...
    抹意焦糖半微凉阅读 644评论 0 1