1 parent 6232f15 commit 98c144fCopy full SHA for 98c144f
1 file changed
happy_number.py
@@ -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