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

242.有效的字母异位词

题目链接

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        record = [0] * 26
        for i in s:
            #并不需要记住字符a的ASCII,只要求出一个相对数值就可以了
            record[ord(i) - ord("a")] += 1
        for i in t:
            record[ord(i) - ord("a")] -= 1
        for i in range(26):
            if record[i] != 0:
                #record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。
                return False
        return True
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import defaultdict
        
        s_dict = defaultdict(int)
        t_dict = defaultdict(int)
        for x in s:
            s_dict[x] += 1
        
        for x in t:
            t_dict[x] += 1
        return s_dict == t_dict
class Solution(object):
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import Counter
        a_count = Counter(s)
        b_count = Counter(t)
        return a_count == b_count

1.python中没有char类型,因此不能写成hashTable[t[j]-‘a’]-=1,可以使用 ord 函数将字符转换为对应的 ASCII 码
2.不要写for j in range(len(t))
3.defaultdict 用于创建一个默认值的字典。
4. collections 模块中的 Counter 类来创建两个字典 a_count 和 b_count,用于统计字符串 s 和 t 中每个字符出现的次数。

349.两个数组的交集

题目链接

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))
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)

1.table[num] = table.get(num, 0) + 1 的作用是:

如果 num 不存在于字典 table 中,则将 num 添加到字典,并将其对应的值设为 1,表示 num 在列表中出现了 1 次。

如果 num 已经存在于字典 table 中,则将 num 对应的值加 1,表示 num 在列表中出现的次数增加了 1 次。

通过这种方式,我们可以统计列表中每个元素出现的次数,并使用字典 table 来存储这些统计结果。在最终的结果中,table 就成为了一个记录列表元素出现次数的字典。
2.通过del table[num],以避免重复计算
3.return list(res)要将set转list

202.快乐数

题目链接

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.不知道怎么取每个位上的数:
n, r = divmod(n, 10): 使用 divmod 函数对 n 进行整除和取余运算,将商赋值给 n,将余数赋值给 r。这样可以从 n 中取出最后一位数字,同时更新 n 的值为剩余的数字。
2.n = sum(int(i) ** 2 for i in str(n))
3.集合用add,数组用append
4.快慢指针的方法没看懂

1.两数之和

1.在 Python 中,enumerate 是一个内置函数,用于遍历一个可迭代对象,并同时获取元素的索引和对应的值。
2.双指针法没懂

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值