【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;
    }
};
相关推荐
向阳@向远方14 分钟前
第二章 简单程序设计
开发语言·c++·算法
github_czy1 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁2 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子2 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z2 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao2 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx3 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背3 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z3 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼4 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode