哈希表算法--学习了哈希表之后的一些总结

如果想要了解什么是哈希表的话,可以看这个文章:

通俗易懂->哈希表详解-CSDN博客

接下来我就用力扣的例题来说明哈希表的算法:

题目一:两数之和:

1. 两数之和 - 力扣(LeetCode)

这道题一开始我们会使用暴力的遍历O(n^2)来解决问题,暴力的代码如下:

作者:力扣官方题解
链接:https://leetcode.cn/problems/two-sum/solutions/434597/liang-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    return {i, j};
                }
            }
        }
        return {};
    }
};

但是这个算法的时间复杂度比较高,所以我们使用了哈希表减少时间复杂度,我们首先使用了一个unordered_map<int, int>来保存某一个值所在的位置,当我们遍历这个哈希表时,对于每一个x,我们都会查找是否有target-x,如果有的话,就返回{target-x,x},如果没有的话,就保存x在哈希表中。

代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hashtable;      // 以值为first,索引为second的哈希表
        for(int i = 0; i < nums.size(); i++){
            auto it = hashtable.find(target - nums[i]);     // 查看有没有与当前数字形成组合的数字
            if(it != hashtable.end()){
                return {it->second, i};     // 返回结果
            }
            hashtable[nums[i]] = i;       // 如果没有的话,先标记这个数字的存在
        }
        return {};
    }
};

第二题:字母字母异位词分组

链接如下:

49. 字母异位词分组 - 力扣(LeetCode)

这个题目真的很难懂,其实他的意思就是说“把字母数量相同的字符串分到一组”,比如['ant','tan']都是有一个a,一个n和一个t。["ate","eat","tea"]都是一个a,一个e,一个t,所以分到一组。

所以我们需要利用以异位词为键,符合异位键的列表为值的哈希表。

1.创建以异位词为键,符合异位键的列表为值的哈希表,和符合异位词的字符串列表。

2.遍历传入的字符串列表,记录每一个字符串的共同点,并加入到这个共同点的列表中

3.将每一个共同点的列表加入到ans中,最后返回ans

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> ans;
        unordered_map<string, vector<string>> hashtable;        // 以异位词为键,符合异位键的列表为值的哈希表
        for(auto &str : strs){
            string res = str;
            sort(res.begin(), res.end());       // 进行排序,获得他们的共同点
            hashtable[res].emplace_back(str);   // 将该字符串加入到这个共同点的列表中。
        }
        
        for(auto it = hashtable.begin(); it != hashtable.end(); it++){
            ans.emplace_back(it->second);
        }
        return ans; 
    }
}; 

第三题:最长连续序列:

128. 最长连续序列 - 力扣(LeetCode)

这道题需要注意的是我们首先需要对这个列表进行去重,然后我们遍历以每一个不同的数字num为开头的情况,获取它们每一次的最长序列长度,最大的那个即为答案。

代码如下:

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> numSet;
        for(const int& num : nums){
            numSet.insert(num);
        }
        int longest = 0;    // 初始化最长子序列长度
        for(const int& num : numSet){       // 开始遍历哈希表
            if(!numSet.count(num-1)){
                // 如果num-1不在哈希表中
                int currentNum = num;   // 表示当前数字作为子序列的头
                int currentStreak = 1;  // 当前连续序列长度
                while(numSet.count(currentNum + 1)){
                    // 如果数字+1在哈希表中,更新当前连续序列长度
                    currentNum++;
                    currentStreak++;
                }
                longest = max(currentStreak, longest);      // 更新最长连续序列长度
            }
        }

        return longest;
    }
};

写在最后:

我们可以在这里学习C++知识:

0voice · GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值