算法-哈希表

1.有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例 1: 输入: s = "anagram", t = "nagaram" 输出: true

示例 2: 输入: s = "rat", t = "car" 输出: false

class Solution:
    def isAnagram(self,s:str,t:str) -> bool:
        res = [0] * 26
        if len(s) != len(t):
            return False
        for i in range(len(s)):
            res[ord(s[i]) - ord('a')] += 1
        for j in range(len(t)):
            res[ord(t[j]) - ord('a')] -= 1
        return res == [0] * 26

2.两个数组的交集

class Solution:
    def intersection(self,nums1,nums2):
        return list(set(nums1) & set(nums2))

3.快乐数

「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为  1,那么这个数就是快乐数。

class Solution:
    def isHappy(self,n):
        def calculate_happy(num):
            sum_ = 0
            #从个位开始依次取,平方求和
            while num:
                sum_ += (num % 10) ** 2
                num //= 10
            return sum_

        res = set()

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

4.两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

class Solution:
    def towSum(self,nums,target):
        res = dict()

        for idx,num in enumerate(nums):
            if target - num not in res:
                res[num] = idx
            else:
                return [res[target - num],idx]

5.四数相加

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。

class Solution:
    def fourSumCount(self,nums1,nums2,nums3,nums4):
        dic = dict()
        for n1 in nums1:
            for n2 in nums2:
                if n1 + n2 in dic:
                    dic[n1 + n2] += 1
                else:
                    dic[n1 + n2] = 1

        count = 0
        for n3 in nums3:
            for n4 in nums4:
                key = -n3 - n4
                if key in dic:
                    count += dic[key]
        return count

6.赎金信

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

class Solution:
    def canConstruct(self,ransomNote,magazine):
        arr = [0] * 26
        if len(ransomNote) > len(magazine):
            return False
        for s in magazine:
            arr[ord(s) - ord('a')] += 1

        for s in ransomNote:
            if arr[ord(s) - ord('a')] == 0:
                return False
            else:
                arr[ord(s) - ord('a')] -= 1
        return True

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值