File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Problem Link: https://leetcode.com/problems/happy-number/
3
+
4
+ Write an algorithm to determine if a number is "happy".
5
+ A happy number is a number defined by the following process: Starting with any positive integer,
6
+ replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1
7
+ (where it will stay), or it loops endlessly in a cycle which does not include 1.
8
+ Those numbers for which this process ends in 1 are happy numbers.
9
+
10
+ Example:
11
+ Input: 19
12
+ Output: true
13
+
14
+ Explanation:
15
+ 12 + 92 = 82
16
+ 82 + 22 = 68
17
+ 62 + 82 = 100
18
+ 12 + 02 + 02 = 1
19
+ """
20
+ class Solution (object ):
21
+ def isHappy (self , n ):
22
+ """
23
+ :type n: int
24
+ :rtype: bool
25
+ """
26
+ while n > 6 :
27
+ nextN = 0
28
+ while (n ):
29
+ nextN += (n % 10 ) * (n % 10 )
30
+ n //= 10
31
+ n = nextN
32
+ return n == 1
You can’t perform that action at this time.
0 commit comments