day 6 第三章 哈希表

day 6 第三章 哈希表
*
day 1 任务以及具体安排:训练营一期day 1
*
day 2 任务以及具体安排:day 2 第一章数组
*
day 3 任务以及具体安排:day 3 第二章 链表
*
day 4 任务以及具体安排:day 4 第二章 链表

今日任务

* 

哈希表理论基础
*
242.有效的字母异位词
*
349. 两个数组的交集
*
202. 快乐数
*

  1. 两数之和

详细布置
哈希表理论基础

建议:大家要了解哈希表的内部实现原理,哈希函数,哈希碰撞,以及常见哈希表的区别,数组,set 和map。

什么时候想到用哈希法,当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法。 这句话很重要,大家在做哈希表题目都要思考这句话。

文章讲解:https://programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

242.有效的字母异位词

建议: 这道题目,大家可以感受到 数组 用来做哈希表 给我们带来的遍历之处。

题目链接/文章讲解/视频讲解: https://programmercarl.com/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.html

  1. 两个数组的交集

建议:本题就开始考虑 什么时候用set 什么时候用数组,本题其实是使用set的好题,但是后来力扣改了题目描述和 测试用例,添加了 0 <= nums1[i], nums2[i] <= 1000 条件,所以使用数组也可以了,不过建议大家忽略这个条件。 尝试去使用set。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0349.%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86.html
202. 快乐数

建议:这道题目也是set的应用,其实和上一题差不多,就是 套在快乐数一个壳子

题目链接/文章讲解:https://programmercarl.com/0202.%E5%BF%AB%E4%B9%90%E6%95%B0.html

  1. 两数之和

建议:本题虽然是 力扣第一题,但是还是挺难的,也是 代码随想录中 数组,set之后,使用map解决哈希问题的第一题。

建议大家先看视频讲解,然后尝试自己写代码,在看文章讲解,加深印象。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0001.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.html

class Solution {
public:
    bool isAnagram(string s, string t) {
        int len = s.size();
        if (len != t.size()) return false;
        vector<int> ht(26, 0);
        for (auto &ch : s) {
            ht[ch - 'a'] ++;
        }
        for (auto &ch : t) {
            ht[ch - 'a'] --;
        }
        for (int i = 0; i < 26; i ++) {
            if (ht[i] != 0) return false;
        }
        return true;
    }
};
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> nums1_set(nums1.begin(), nums1.end());
        unordered_set<int> result;
        for (auto &num : nums2) {
            if (nums1_set.find(num) != nums1_set.end()) {
                result.insert(num);
            }
        }
        return vector<int>(result.begin(), result.end());
    }
};
class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> nums;
        while (n != 1) {
            if (nums.find(n) != nums.end()) return false;
            nums.insert(n);
            n = sumOfSquares(n);
        }
        return true;
    }
    int sumOfSquares(int n) {
        int sum = 0;
        while (n != 0) {
            sum += pow(n % 10, 2);
            n /= 10;
        }
        return sum;
    }
};
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> num_index;
        for (int i = 0; i < nums.size(); ++ i) {
            auto iter = num_index.find(target - nums[i]);
            if (iter != num_index.end()) {
                return vector<int>{i, iter->second};
            }
            num_index.insert(pair<int, int>(nums[i], i));
        }
        return vector<int>{};
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值