【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;
    }
};
相关推荐
max50060016 分钟前
实时多模态电力交易决策系统:设计与实现
图像处理·人工智能·深度学习·算法·音视频
其古寺27 分钟前
贪心算法与动态规划:数学原理、实现与优化
算法·贪心算法·动态规划
rit84324991 小时前
基于灰狼算法(GWO)优化支持向量回归机(SVR)参数C和γ的实现
c语言·算法·回归
蒋士峰DBA修行之路1 小时前
实验五 静态剪枝
数据库·算法·剪枝
蒋士峰DBA修行之路1 小时前
实验六 动态剪枝
数据库·算法·剪枝
Tim_102 小时前
【算法专题训练】20、LRU 缓存
c++·算法·缓存
B612 little star king2 小时前
力扣29. 两数相除题解
java·算法·leetcode
野犬寒鸦2 小时前
力扣hot100:环形链表(快慢指针法)(141)
java·数据结构·算法·leetcode·面试·职场和发展
时光追逐者2 小时前
C# 哈希查找算法实操
算法·c#·哈希算法
Jasmine_llq3 小时前
《P3825 [NOI2017] 游戏》
算法·游戏·枚举法·2-sat 算法·tarjan 算法·邻接表存储