两数求和
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hash=dict(nums,range(len(nums)))
for i in range(len(nums)):
num=target-nums[i]
if hash.get(num):
if i !=hash[num]:
return [i,hash[num]]
快乐数
class Solution:
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
visited=set()
while n!=1:
n=sum(int(i)**2 for i in str(n))
if n in visited:
return False
else:
visited.add(n)
else:
return True