2024/4/22 代码随想录Day 6:哈希表理论基础 242.有效的字母异位词 349. 两个数组的交集

本文介绍了如何使用C++和Python中的哈希表理论解决有效字母异位词问题,并对比了数组和哈希表在存储和查找交集中的效率。还讨论了unordered_set在Python中的应用以及不同方法的优劣。
摘要由CSDN通过智能技术生成

2024/4/22 代码随想录Day 6:哈希表理论基础 242.有效的字母异位词 349. 两个数组的交集

哈希表理论基础

随想录文章链接

242.有效的字母异位词

数组 用来做哈希表 给我们带来的便利之处

题目链接 242 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

第一次提交

随想录
视频链接
使用数组作为哈希表的容器

c++ version
class Solution {
public:
    bool isAnagram(string s, string t) {
        int count[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            count[s[i] - 'a'] ++;
        }
        for (int i = 0; i < t.size(); i++) {
            count[t[i] - 'a'] --;
        }
        for (int i = 0; i <26; i++) {
            if (count[i] != 0) return false;
        }
        return true;
    }
};
Python Version
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

349. 两个数组的交集

题目链接 242 给定两个数组 nums1 和 nums2 ,返回 它们的
交集
。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

第一次提交

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set, res;
        vector<int> result;
        for (auto i : nums1) set.insert(i);
        for (auto i : nums2) {
            if (set.count(i)) res.insert(i);
        }
        for (int i : res) result.push_back(i);
        return result;
    }
};

学习优秀题解

直接使用set 不仅占用空间比数组大,而且速度要比数组慢,set把数值映射到key上都要做hash计算的。

不要小瞧 这个耗时,在数据量大的情况,差距是很明显的。
随想录
视频链接
哈希表跟 vector 的互相转化

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set(nums1.begin(),nums1.end()), res;
        for (int i : nums2) {
            if (set.count(i)) res.insert(i);
        }
        vector<int> result(res.begin(),res.end());
        return result;
    }
};

python 中字典的应用
table.get(num, 0):
The expression table.get(num, 0) retrieves the value associated with the key num from the hash table table. If the key is not found in the hash table, it returns the default value 0.

使用集合存放结果

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:#table.get(num, 0):
                res.add(num)
        return list(res)

优雅的一行写法

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值