【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;
    }
};
相关推荐
二进制person4 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6664 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way4 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
.30-06Springfield5 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
凌肖战7 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
weixin_478689767 小时前
十大排序算法汇总
java·算法·排序算法
luofeiju8 小时前
使用LU分解求解线性方程组
线性代数·算法
SKYDROID云卓小助手8 小时前
无人设备遥控器之自动调整编码技术篇
人工智能·嵌入式硬件·算法·自动化·信号处理
ysa0510308 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
GEEK零零七9 小时前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列