ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

leetcode(26)哈希表系列题目

2022-06-08 13:34:53  阅读:252  来源: 互联网

标签:26 return int Counter List records dict 哈希 leetcode


242. 有效的字母异位词

哈希表方法,可适应更大规模字符集

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dict_ = {}
        for ch in s:
            if ch in dict_:
                dict_[ch] += 1
            else:
                dict_[ch] = 1
        for ch in t:
            if ch in dict_:
                dict_[ch] -= 1
            else:
                dict_[ch] = 1
        for v in dict_.values():
            if v != 0:
                return False
        return True

或者取巧用Counter()函数

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dict_s, dict_t = Counter(s), Counter(t)
        if dict_s == dict_t:
            return True
        else:
            return False

349. 两个数组的交集

注意:if hash.get(n):如果键n对应的值为0则返回False;而if n in hash:会返回True

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        res = []
        hash = {}
        for n in nums1:
            if n not in hash:
                hash[n] = 1
        for n in nums2:
            # if n in hash:
            if hash.get(n):
                hash[n] = 0
                res.append(n)
        return res

取巧的做法:

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        res = []
        set1, set2 = set(nums1), set(nums2)
        for num in set1:
            if num in set2:
                res.append(num)
        return res

1. 两数之和

用枚举更方便,就不需要通过索引再去取当前位置的值

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        records = dict()
        for idx, val in enumerate(nums):
            if target - val in records:
                return [records[target - val], idx]
            else:
                records[val] = idx
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dict_ = {}
        for i in range(len(nums)):
            if target - nums[i] in dict_:
                return [dict_[target - nums[i]], i]
            else:
                dict_[nums[i]] = i

454. 四数相加 II

把num1和num2看作一个整体,num3和num4看作一个整体

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        res = 0
        records = dict()
        for i in nums1:
            for j in nums2:
                if i + j not in records:
                    records[i + j] = 1
                else:
                    records[i + j] += 1
        for i in nums3:
            for j in nums4:
                if -(i + j) in records:
                    res += records[-(i + j)]
        return res

383. 赎金信

Counter()函数可以直接相减
若ransomNote中的字符在magazine没有出现则会返回Counter({'a': 1})
否则返回Counter(),即为空的

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        if len(ransomNote) > len(magazine):
            return False
        return not Counter(ransomNote) - Counter(magazine)
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        recordsR, recordsM = Counter(ransomNote), Counter(magazine)
        for s in recordsR:
            if recordsR[s] > recordsM[s]:
                return False
        return True

标签:26,return,int,Counter,List,records,dict,哈希,leetcode
来源: https://www.cnblogs.com/ttyangY77/p/16352966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有