Leetcode 算法题 打卡day06

Leetcode 算法 打卡day6

哈希表基础

哈希表是根据关键码的值直接进行访问的数据结构。

哈希函数: a d d r e s s = H ( k e y ) address = H(key) address=H(key) ,哈希函数可以根据key计算存储地址,而哈希表是基于哈希函数简历的一种查找表。

查询一个元素是否在一个表内,使用枚举的方法时间复杂度是 O ( n ) O(n) O(n), 使用哈希表时间复杂度是 O ( 1 ) O(1) O(1)

哈希碰撞

例如两个key经过映射到了同一个地址。解决方案有拉链法线性探针法

常见的哈希结构

  1. 数组
  2. 集合 set
  3. map

在C++中
std::unordered_set底层实现为哈希表,std::set 和std::multiset 的底层实现是红黑树,红黑树是一种平衡二叉搜索树,所以key值是有序的,但key不可以修改,改动key值会导致整棵树的错乱,所以只能删除和增加。

std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。同理,std::map 和std::multimap 的key也是有序的。

总结

哈希表可以实现快速查新一个元素是都出现在集合里,所以当题目出先需要快速查询元素是否在集合时,要考虑哈希法。

242. 有效的字母异位词

使用哈希表统计每种元素出现的次数即可。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        # 如果两个字符串长度不同必然不是
        if len(s) != len(t):
            return False        
        need = collections.defaultdict(int)
        seen = collections.defaultdict(int)
        for ss in s:
            need[ss] += 1
        for tt in t:
            seen[tt] += 1
        if need == seen:
            return True
        return False

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length() != t.length()) return false;
        unordered_map<char, int> dic1, dic2; # 哈希表
        for (char c : s){
            dic1[c] += 1;
        }
        for (char c : t){
            dic2[c] += 1;
        }
        if (dic1 == dic2){
            return true;
        }
        return false;

    }
};

349. 两个数组的交集

将两个数组存到两个set中,遍历较短的set,查询较长的set是否包含元素。

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        res = []
        from collections import defaultdict
        seen = defaultdict(int)
        for x in nums1:
            seen[x] += 1
        for y in nums2:
            if y in seen:
                res.append(y)
                seen[y] -= 1
                seen.pop(y)
        return res

202. 快乐数

class Solution:
    def getsum(self, b):
        sumb = 0
        strb = str(b)
        for x in strb:
            x = int(x)
            sumb = sumb + x**2
        return sumb

    def isHappy(self, n: int) -> bool:
        from collections import defaultdict as ddt
        # 其实核心在于什么时候停止循环
        # 如果出现之前出现过的和那么说明该停止了 后面将是无限循环
        seen = ddt(int)
        while True:
            if n == 1:
                return True
            if n in seen:
                return False
            else:
                seen[n] += 1
            n = self.getsum(n)

1. 两数之和

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        seen = {}
        for i in range(len(nums)):
            need = target - nums[i]
            if need in seen:
                return [seen[need], i]
            else:
                seen[nums[i]] = i
        return 
  • 47
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值