算法:常见的哈希表算法

本文总结的是关于哈希表常见的算法

哈希表其实就是一个存储数据的容器,所以其实它本身的算法难度并不高,只是利用哈希表可以对于一些场景进行优化

两数之和

在这里插入图片描述

class Solution 
{
   
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
   
        // 把数都丢到哈希表中,哈希表的意义是元素及其对应的下标
        unordered_map<int, int> hash;
        for(int i = 0; i < nums.size(); i++)
        {
   
            int x = target - nums[i];
            if(hash.count(x)) return {
   hash[x], i};
            hash[nums[i]] = i;
        }
        return {
   -1, -1};
    }
};

判断是否互为字符重排

在这里插入图片描述

class Solution 
{
   
public:
    bool CheckPermutation(string s1, string s2) 
    {
   
        int hash1[26] = {
   0};

        for(auto e : s1)
            hash1[e - 'a']++;
        for(auto e : s2)
            hash1[e - 'a']--;
        
        for(int i = 0; i < 26; i++)
            if(hash1[i])
                return false;
        
        return true;
    }
};

存在重复元素

在这里插入图片描述

class Solution 
{
   
public:
    bool containsDuplicate(vector<int>& nums) 
    {
   
        unordered_map<int, int> dict;
        for(auto& e : nums)
            dict[e]++;
        for(auto& e : nums)
            if(dict[e] != 1)
                return true;
        return false;
    }
};

其实是可以优化的,不需要看出现了几次,这个题只关心有没有的问题,因此使用set就可以了

class Solution 
{
   
public:
    bool containsDuplicate(vector<int>& nums) 
    {
   
        unordered_set<int> hash;
        for(auto& e : nums)
            if(hash.count(e)) return true;
            else hash.insert(e);
        return false;
    }
};

存在重复元素

在这里插入图片描述

class Solution
{
   
public:
	bool containsNearbyDuplicate(vector<int>& nums, int k)
	{
   
		unordered_map<int, int> hash;
		for (int i = 0; i < nums.size(); i++)
		{
   
			if (hash.count(nums[i]) && abs(hash[nums[i]] - i) <= k)
			{
   
				return true;
			}
			else
			{
   
				hash[nums[i]] = i;
			}
		}
		return false;
	}
};

字母异位词分组

在这里插入图片描述

class Solution 
{
   
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) 
    {
   
        unordered_map<string, vector<string>> hash;

        // 把数据拿进去
        for(auto s : strs)
        {
   
            string tmp = s;
            sort(tmp.begin(), tmp.end());
            hash[tmp].push_back(s);
        }

        // 再取出来
        vector<vector<string>> res;
        for(auto& [x, y] : hash)
        {
   
            res.push_back(y);
        }

        return res;
    }
};

这里主要是说明,哈希表中是可以存储其他内容的,同时也体现出泛型编程的强大之处

相关推荐

  1. 算法

    2023-12-12 07:28:04       17 阅读
  2. 常见算法及其应用场景

    2023-12-12 07:28:04       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-12 07:28:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-12 07:28:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-12 07:28:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-12 07:28:04       20 阅读

热门阅读

  1. Docker与K8s的区别

    2023-12-12 07:28:04       38 阅读
  2. x的平方根算法(leetcode第69题)

    2023-12-12 07:28:04       38 阅读
  3. Flask存储在内存中的密钥被读取

    2023-12-12 07:28:04       38 阅读
  4. MATLAB 2021b 安装教程

    2023-12-12 07:28:04       48 阅读
  5. 微服务学习二

    2023-12-12 07:28:04       44 阅读
  6. 洛谷 P8628 [蓝桥杯 2015 国 AC] 穿越雷区

    2023-12-12 07:28:04       47 阅读
  7. 【Fiddler】IDEA配置Fiddler

    2023-12-12 07:28:04       36 阅读
  8. 面试经典150题(14)

    2023-12-12 07:28:04       39 阅读