728x90
class Solution:
def isHappy(self, n):
lookup = {}
while n != 1 and n not in lookup:
lookup[n] = True
n = self.nextNumber(n)
return n == 1
def nextNumber(self, n):
new = 0
for char in str(n):
new += int(char)**2
return new
출처: github.com/jiapengwen/LeetCode/blob/master/Python/happy-number.py
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Count Primes 파이썬 (0) | 2021.04.04 |
---|---|
[LeetCode] Remove Linked List Elements 파이썬 (linked list) (0) | 2021.04.04 |
[LeetCode] Fatorial Trailing Zeros 파이썬 (dp) (0) | 2021.04.03 |
[LeetCode] Excel Sheet Column Number 파이썬 (ord) (0) | 2021.04.03 |
[LeetCode] Majority Element 파이썬 (dict, Counter, median) (0) | 2021.04.02 |
댓글