Thought:
Sum the squares of each number of bits for the input data
To get the result, if it's 1, return to the real, or the result is recursive.
When to return a fake:
Return false instructions into the infinite loop.
When will there be infinite loops?
The result of a one-time squared sum, which has been obtained before, is infinitely cyclic.
So, I save the results every time I get it, and if I find one that gets the results before, then I'm sure it's an infinite loop. return False
1 classSolution:2 defishappy (self, n):3 """4 : Type N:int5 : Rtype:bool6 """7temp = []8 whileTrue:9n =self.get_add (n)Ten ifn = = 1: One returnTrue A elifNinchTemp: - returnFalse - Else: the temp.append (n) - - defGet_add (self, n): -RET =0 + whilen! =0: -g = n% 10 +RET + = G * * 2 An = Int (N/10) at returnret - - - - if __name__=='__main__': -s =solution () in Print(S.ishappy (19))
Leetcode 202. Happy number Python implementation