Skip to content

Commit 98c144f

Browse files
committed
快乐数
1 parent 6232f15 commit 98c144f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

happy_number.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param {int} n an integer
5+
# @return {boolean} true if this is a happy number or false
6+
def isHappy(self, n):
7+
# Write your code here
8+
ret = {}
9+
while True:
10+
digits = []
11+
while n > 0:
12+
digits.append(n % 10)
13+
n /= 10
14+
n = 0
15+
for d in digits:
16+
n += d * d
17+
if n == 1:
18+
return True
19+
if n in ret:
20+
return False
21+
else:
22+
ret[n] = True

0 commit comments

Comments
 (0)