1.对于hash的认识
hash 函数:value = hash(key),有点像python中的dict(若理解有误,望指正)。
常用的hash table:是根据关键码值(key value)而直接进行访问的数据结构,通过将(key value)映射到表中一个位置,以加快查找速度。
hash table设计与思想见博客:见链接1
2. LeetCode #1. two sumLeetCode
问题如图:
solution demo:
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
aa,i = {},0
while i<len(nums):
ab = target-nums[i]
if ab in aa:
return [aa[ab],i]
aa[nums[i]] = i
i += 1
3 LeetCode #2. Happy Number
问题如图:
solution demo:
class Solution:
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
num_dict = {}
while n!=1:
num_dict[n] = False
n = sum([int(each)**2 for each in str(n)])
if n in num_dict:
return False
return True
参考:
链接1:https://blog.csdn.net/wilsonsong1024/article/details/82318487