Question: https://leetcode.com/problems/happy-number/
Question Name: Happy Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution(object): def _nextNumber(self, n): """ :type n: int :rtype: bool """ result = 0 while n != 0: result += (n % 10) **2 n /= 10 return result def isHappy(self, n): """ :type n: int :rtype: bool """ met = set() while not n in met: if n == 1: return True met.add(n) n = self._nextNumber(n) return False |