算法笔记DAY7

1.Leetcode 242.有效的字母异位词

1.理解红黑树哈希的区别

2.理解stl库中哈希的几个容器

集合:std::set std::multiset std::unordered_set
映射:std::map std::multimap std::unordered_map
理解哪两个是无序的?-> 搞清楚红黑树 和 哈希表之后就很容易得出 底层为红黑树的为有序 底层为哈希表的std::unordered_map std::unordered_set 是无序
其次记住 std::multiset std::multimap 的key值可以重复

3.思考为什么使用哈希表 用其他方法是不是也可以 ,但是时间复杂度?

4.唯一算是难点的一步为把字符转换成整数存储为key值。

5.其他的就基本思考下就能做出

	bool isAnagram(string s, string t) {
        int index = 0;
        int ssize = s.size();
        int tsize = t.size();
        if(ssize != tsize) return false;
        int hash[26] = {0};
        while(ssize--){
            int i = s[index++] - 'a';
            hash[i]++;
        }
        index = 0;
        while(tsize--){
            int i = t[index++] - 'a';
            hash[i]--;
            if(hash[i] < 0) return false;
        }
        return true;
    }

2.Leetcode 349.两个数组的交集

1.本题考察的主要是对stl容器的用法
unordered_set容器是无序非重复的 在此题中的用法为去重

2.寻找交集
考察find用法

3.总结:难度不大 主要熟悉stl容器和哈希

	vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> res;
        unordered_set<int> nums1Set(nums1.begin(),nums1.end());
        for(int num:nums2){
            if(nums1Set.find(num) != nums1Set.end()){
                res.insert(num);
            }
        }
        return vector<int>(res.begin(),res.end());
    }
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值