202 Happy Number
Write an algorithm to determine if a number ishappy.
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
通过计算发现,如果不是happy number的数,算着算着会出现重复数字,所以可以用一个hashset来保存出现过的数,如果发现,就表明这个不是happy number了。
2刷重写的时候,好像找到更容易理解/记住的方法:
实现时要注意代码写法,不知为毛得这样写才不会死循环。
Last updated