-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path202_happyNumber.py
43 lines (39 loc) · 1.13 KB
/
202_happyNumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
seen = set()
while (True):
sumOfSquaredDigits = 0
while n > 0:
digit = n % 10
n = n // 10
sumOfSquaredDigits += digit**2
if sumOfSquaredDigits == 1:
return True
if sumOfSquaredDigits in seen:
return False
seen.add(sumOfSquaredDigits)
n = sumOfSquaredDigits
# seen = []
# while (True):
# print (seen)
# digits = self.getDigits(n)
# total = 0
# print ("Digits: " + str(digits))
# for i in digits:
# total += int(pow(i,2))
# if (total in seen):
# return False
# if (total == 1):
# return True
# seen.append(total)
# n = total
# def getDigits(self, n):
# digits = []
# while (n > 0):
# digits.append(n%10)
# n = n//10
# return digits