数据结构学习day1:hash table

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值