update project structure
This commit is contained in:
26
leetcode_py/202.py
Normal file
26
leetcode_py/202.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Happy Number
|
||||
# Repeat the process until the number equal to 1, these are happy
|
||||
# OR LOOP ENDLESS in a cycle, these are not happy
|
||||
|
||||
|
||||
class Solution:
|
||||
def isHappy(self, n: int, status=None) -> bool:
|
||||
if status is None:
|
||||
status = []
|
||||
|
||||
str_n = str(n)
|
||||
total = 0
|
||||
for number in str_n:
|
||||
total += int(number) ** 2
|
||||
if total == 1:
|
||||
return True
|
||||
if str(total) in status:
|
||||
return False
|
||||
|
||||
status.append(str(total))
|
||||
return self.isHappy(total, status)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
number = 13
|
||||
print(Solution().isHappy(number))
|
||||
Reference in New Issue
Block a user