【C++算法】哈希表

哈希表介绍:

1.哈希表是什么?

存储数据的容器

2.哈希表有什么用?

"快速"查找某个元素------O(N)

3.什么时候使用哈希表?

频繁的查找某一个数的时候,频繁也可以使用二分(有序)

4.怎么用哈希表?

1.容器(哈希表)

2.用数组模拟简易哈希表

  • 字符串中的"字符"
  • 数据范围很小的时候

俩数之和

  • 题目链接

俩数之和https://leetcode.cn/problems/two-sum/submissions/565694047/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hashi;
        for(int i = 0; i < nums.size(); i++)
        {
             if(hashi.find(target - nums[i])!= hashi.end())
             {
                  return {hashi[target - nums[i]], i};
             }
            hashi[nums[i]] = i;
        }
        return {-1, -1};
    }
};

判定是否互为字符重排

  • 题目链接

判定是否互为字符重排https://leetcode.cn/problems/check-permutation-lcci/description/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    bool CheckPermutation(string s1, string s2) 
    {
        if(s1.size() != s2.size()) return false;
        int hash[26] = { 0 };
        for(int i = 0; i < s1.size(); i++)
        {
            hash[s1[i] - 'a']++;
        }

        for(int i = 0; i < s2.size(); i++)
        {
            hash[s2[i] - 'a']--;
            if(hash[s2[i] - 'a'] < 0) return false;
        }

        return true;

    }
};

存在重复元素I

  • 题目链接

存在重复元素Ihttps://leetcode.cn/problems/contains-duplicate/description/

  • 算法原理

解法:哈希表

  • 代码展示
cpp 复制代码
class Solution 
{
public:
    bool containsDuplicate(vector<int>& nums) 
    {
        unordered_set<int> hash;
        for(int i = 0; i < nums.size(); i++)
        {
            if(hash.count(nums[i])) return true;
            else hash.insert(nums[i]);
        }

        return false;
    }
};

存在重复元素II

  • 题目链接

存在重复元素IIhttps://leetcode.cn/problems/contains-duplicate-ii/description/

  • 算法原理
  • 代码展示
cpp 复制代码
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]) && i - hash[nums[i]] <= k) return true;
            else hash[nums[i]] = i;
        }
        return false;
    }
};

字母异位词分组

  • 题目链接

字母异位词分组https://leetcode.cn/problems/group-anagrams/

  • 算法原理
  • 代码展示
cpp 复制代码
class Solution 
{
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) 
    {
        unordered_map<string, vector<string>> hash;
        for(int i = 0; i < strs.size(); i++)
        {
            // 排序
            string tmp = strs[i];
            sort(tmp.begin(), tmp.end());
            // 添加
            hash[tmp].push_back(strs[i]);
        }

        vector<vector<string>> ret;
        for(auto& [x, y] : hash)
        {
            ret.push_back(y);
        }

        return ret;
    }
};
相关推荐
葫三生27 分钟前
如何评价《论三生原理》在科技界的地位?
人工智能·算法·机器学习·数学建模·量子计算
拓端研究室3 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
随缘而动,随遇而安5 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
IT古董5 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
水木兰亭8 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
Jess079 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁9 小时前
选择排序算法详解
数据结构·算法·排序算法
xindafu9 小时前
代码随想录算法训练营第四十二天|动态规划part9
算法·动态规划
xindafu9 小时前
代码随想录算法训练营第四十五天|动态规划part12
算法·动态规划
ysa05103010 小时前
Dijkstra 算法#图论
数据结构·算法·图论