Created
April 2, 2020 16:41
-
-
Save munguial/9e9af36a6469287aa8fecf8c15f97335 to your computer and use it in GitHub Desktop.
Day 2 - Happy number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3284/ | |
class Solution: | |
def isHappy(self, n: int) -> bool: | |
knownNumbers = {} | |
while True: | |
if n == 1: | |
return True | |
if n in knownNumbers: | |
return False | |
knownNumbers[n] = None | |
n = self.calculateNextNumber(n) | |
return False | |
def calculateNextNumber(self, n: int) -> int: | |
strDigits = list(str(n)) | |
return sum((int(digit) * int(digit)) for digit in strDigits) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment