Skip to content

Commit

Permalink
Merge pull request #94 from JohannesDev/202.-Happy-Number
Browse files Browse the repository at this point in the history
Add 202. Happy Number in python
  • Loading branch information
Rohit0301 authored Oct 4, 2022
2 parents 8bc179e + cb0f78a commit b1036c0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Leetcode/Happy number/Happy number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def isHappy(self, n: int) -> bool:
seen_sums = set()
sum = n

while sum != 1:
sum = 0
for char in str(n):
num = int(char)
sum = sum + num * num

# cycle detected
if sum in seen_sums:
return False

seen_sums.add(sum)
n = sum

return True

0 comments on commit b1036c0

Please sign in to comment.