![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
哈希表
princey2100
这个作者很懒,什么都没留下…
展开
-
Leetcode 383. 赎金信 Ransom Note - Python 哈希法
class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: hash = [0] *26 for c in magazine: hash[ord(c)- ord('a')] += 1 for c in ransomNote: if hash[ord(c)-ord('a')] == .原创 2022-02-28 12:09:34 · 274 阅读 · 0 评论 -
Leetcode 454. 四数相加 II 4Sum II - Python 哈希法
class Solution: def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: hash = dict()#统计nums1和nums2所有potential sum 的次数,将其放到哈希表中 for n1 in nums1: for n2 in nums2: .原创 2022-02-28 11:38:31 · 184 阅读 · 0 评论 -
Leetcode 1.两数之和 Two Sum - Python 哈希法
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: pool = dict() for idx, num in enumerate(nums): if target - num not in pool: pool[num] = idx else: r.原创 2022-02-28 10:44:56 · 314 阅读 · 0 评论 -
Leetcode 202. 快乐数 Happy Number - Python 哈希法
class Solution: def isHappy(self, n: int) -> bool: sum_pool = set() num = str(n) sum = 0 while sum != 1: sum = 0 for i,c in enumerate(num): sum += pow(int(c),2) if.原创 2022-02-28 10:21:59 · 234 阅读 · 0 评论 -
Leetcode 349.两个数组的交集 Intersection of Two Arrays - Python 哈希法
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))当数值较大时,用数组做哈希表是不明智的选择。此时可以选择set集合数据结构了。set的元素是无序的,不可更改的,无索引下标的。详情看Python Set 的W3School具体介绍...原创 2022-02-27 23:00:02 · 293 阅读 · 0 评论 -
Leetcode 1002. 查找共用字符 Find Common Characters - Python 哈希法
class Solution: def commonChars(self, words: List[str]) -> List[str]: if not words : return [] result = [] hash = [0]*26#记录数组中第一个字符串各字母出现频率 for i, c in enumerate(words[0]): hash[ord(c)-ord('a')] += 1#记录.原创 2022-02-26 23:20:00 · 298 阅读 · 0 评论