您的位置:首页 > 编程语言 > Python开发

[Leetcode with python] 202. Happy Number (# Hash table)

2019-02-11 17:53 477 查看

题目

https://leetcode.com/problems/happy-number/

解题思路

用字典储存出现过的数字。若重复出现,则返回False;若等于1,则返回True。

代码

class Solution(object):
def cal_square_sum(self, n):
num_l = []
while n > 0:
num_l.append(n%10)
n = n/10
res = 0
for i in num_l:
res += i**2
return res

def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
d = {}
while n not in d:
d[n] = 0
n = self.cal_square_sum(n)
if n == 1:
return True
return False
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: