-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0202-happy-number.py
41 lines (31 loc) · 1.21 KB
/
0202-happy-number.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
class Solution:
# O(log n) time and O(1) space
# d (number of digits) = (Log(10) n) + 1 ~ O(log n)
def sumOfSquares(self, n: int) -> int:
total = 0
while n > 0:
total += (n % 10) ** 2
n = n // 10
return total
# O(k * log n) time and O(k) space
# k is the number of iterations required to determine whether the number is happy or not.
def isHappy1(self, n: int) -> bool:
squares = set()
last_sum = self.sumOfSquares(n)
while last_sum != 1:
if last_sum in squares:
return False
squares.add(last_sum)
last_sum = self.sumOfSquares(last_sum)
return True
# O(k * log n) time and O(1) space
# k is the number of iterations required to determine whether the number is happy or not.
def isHappy2(self, n: int) -> bool:
slow = n
fast = self.sumOfSquares(n)
while fast != slow:
slow = self.sumOfSquares(slow)
fast = self.sumOfSquares(self.sumOfSquares(fast))
return True if fast == 1 else False
def isHappy(self, n: int) -> bool:
return self.isHappy2(n)