代码随想录算法训练营第六天| 242.有效的字母异位词、349.两个数组的交集、202.快乐数、1.两数之和

242.有效的字母异位词

思路

  1. 数组就是一个简单哈希表,而且这道题目中字符串只有小写字符,那么就可以定义一个数组,来记录字符串里字符出现的次数。
  2. 字符a到字符z的ASCII是26个连续的数值,所以字符a映射为下标0,相应的字符z映射为下标25。ord()函数是Python一个内置函数,用于获取字符的Unicode码点(整数表示)。
  3. 开始想的是两个字符串要存到两个哈希表里,再比较两个一不一致,看了解析发现只需要一个哈希表,遍历两遍进行加和减的操作,再看哈希表是否都为0。

代码

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        record = [0] * 26
        for c in s:
            record[ord(c) - ord('a')] += 1

        for c in t:
            record[ord(c) - ord('a')] -= 1

        for i in range(26):
            if record[i] != 0:
                return False

        return True

349.两个数组的交集

思路

  1. 使用数组来做哈希的题目,是因为题目都限制了数值的大小。
  2. 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费。
  3. 直接使用set 不仅占用空间比数组大,而且速度要比数组慢,set把数值映射到key上都要做hash计算的。在数据量大的情况,差距是很明显的。

代码

使用字典和集合

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
    # 使用哈希表存储一个数组中的所有元素
        table = {}
        for num in nums1:
            table[num] = table.get(num, 0) + 1
        
        # 使用集合存储结果
        res = set()
        for num in nums2:
            if num in table:
                res.add(num)
                del table[num]
        
        return list(res)

使用数组

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        count1 = [0]*1001
        count2 = [0]*1001
        result = []
        for i in range(len(nums1)):
            count1[nums1[i]]+=1
        for j in range(len(nums2)):
            count2[nums2[j]]+=1
        for k in range(1001):
            if count1[k]*count2[k]>0:
                result.append(k)
        return result

使用集合

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))

202.快乐数

思路

题目中说了会无限循环,也就是说求和的过程中,sum会重复出现。当遇到要快速判断一个元素是否出现集合里的时候,就要考虑哈希法。

代码

class Solution:
    def isHappy(self, n: int) -> bool:        
        record = set()

        while True:
            n = self.get_sum(n)
            if n == 1:
                return True
            
            # 如果中间结果重复出现,说明陷入死循环了,该数不是快乐数
            if n in record:
                return False
            else:
                record.add(n)

    def get_sum(self,n: int) -> int: 
        new_num = 0
        while n:
            n, r = divmod(n, 10)
            new_num += r ** 2
        return new_num

1.两数之和

思路

  1. 为什么会想到用哈希表:要用一个集合来存放遍历过的元素,然后在遍历数组的时候去询问这个集合,某元素是否遍历过(感觉这个不容易直接想到,要找target-value是不是出现过了)
  2. 哈希表为什么用map:不仅要知道元素有没有遍历过,还要知道这个元素对应的下标
  3. map是用来存什么的,key和value用来存什么的:key来存元素,value来存下标

代码

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        records = dict()

        for index, value in enumerate(nums):  
            if target - value in records:   # 遍历当前元素,并在map中寻找是否有匹配的key
                return [records[target-value], index]
            records[value] = index    # 如果没找到匹配对,就把访问过的元素和下标加入到map中
        return []

每日总结

感觉这个部分的题很多都有多种解法,时间关系先只练了一种,以后二刷可以尝试不同写法。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值