LintCode Q488 Happy Number in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2016年11月30日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
# @param {int} n an integer
# @return {boolean} true if this is a happy number or false
def isHappy(self, n):
# Write your code here
# No Happy Number has a pattern
# the sum of the squares is looping
# if the same sum appears agaiin, its not a happy number
if n == 1 or n == 7:
return True
else:
if n < 10:
return False
sum = 0
while n > 0:
digit = n % 10
n = n / 10
sum += digit * digit
return self.isHappy(sum)
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。