当前位置: 首页 > news >正文

优先算法——专题十:哈希表

哈希表就是为了快速查找的,查找效率极快

在涉及到查找的算法题中,可以考虑使用哈希思想

两数之和

1. 两数之和 - 力扣(LeetCode)

class Solution {
public:
//解法一:暴力枚举:可以每次固定前面一个元素,再遍历这个元素后面的元素,看加起来有没有等于target的;另外,还有一种暴力枚举:就是从前面开始,固定一个元素,每次从这个元素前面找元素,看加起来有没有等于target的,这种解法是为了解法二的哈希解法// vector<int> twoSum(vector<int>& nums, int target) // {//     int n = nums.size();//     for(int i = 0; i < n; i++)//     {//         int tmp = target - nums[i];//         for(int j = 0; j < i; j++)//         {//             if(nums[j] == tmp)//                 return {i, j};//         }//     }//     return {};// }
//解法二:哈希解法:结合上述第二种暴力枚举,每次固定的元素和前面放入哈希表中的元素相加的值和target比较完之后,将这个固定元素及其下标入哈希,接着往后遍历
//为什么要这样,首先,可以一边比较查找一边构建哈希表;其次若是采用第一中暴力枚举,那么先构建哈希表(因为固定一个元素之后后面的元素不在哈希表无法查找)之后,可能查找时找到自己两次,比如target=8,固定的元素为4,此时在哈希表中查找,因为所有元素都在哈希表内,那么只有一个4时,就把这个固定的位置用来两次,还要特殊判断
//而第结合二种暴力枚举无妨,因为在固定的元素比较完之前,自己还没有入哈希表vector<int> twoSum(vector<int>& nums, int target) {int n = nums.size();unordered_map<int, int> hash;for(int i = 0; i < n; i++){int tmp = target - nums[i];if(hash.count(tmp) != 0)//若是不等于-1,说明前面存在这个元素,直接返回return {i, hash[tmp]};//若是等于0,那么前面没有,入{nums[i], i}进hashhash[nums[i]] = i;}return {};}
};

判断是否为字符重排

面试题 01.02. 判定是否互为字符重排 - 力扣(LeetCode)

class Solution {
public:
//使用两个哈希表// bool CheckPermutation(string s1, string s2) {//     vector<int> hash(26, 0);//     vector<int> hash2(26, 0);//     for(auto ch : s1)//     {//         hash[ch - 97]++;//     }//     for(auto ch : s2)//     {//         hash2[ch - 97]++;//     }//     for(int i = 0; i < 26; i++)//     {//         if(hash[i] != hash2[i])//             return false;//     }//     return true;// }
//使用一个哈希表bool CheckPermutation(string s1, string s2) {if(s1.size() != s2.size())return false;//长度不同一定不能重排vector<int> hash(26, 0);for(auto ch : s1){hash[ch - 97]++;}for(auto ch : s2){hash[ch - 97]--;}for(int i = 0; i < 26; i++){if(hash[i] != 0)return false;}return true;}
};

存在重复元素

217. 存在重复元素 - 力扣(LeetCode)

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

存在重复元素

217. 存在重复元素 - 力扣(LeetCode)

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

存在重复元素 II

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]) != 0){if(abs(hash[nums[i]] - i) <= k)return true;}hash[nums[i]] = i;//每次都更新成最新元素,因为找的两个下标应该是最近的}return false;}
};

字母异位词分组

49. 字母异位词分组 - 力扣(LeetCode)

这个题可以体现容器的强大

先判断字符串是否为异位词,若是则将它们放到同一个数组里面;这里可以用哈希表,key值为string,value为string[],因为先将字符串排序了,那么互为异位词的字符串排序之后一定一样,那么就是key值一样,将对应的原词放入这个key值的value也就是数组中

class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {vector<vector<string>> ans;unordered_map<string, vector<string>> hash;for(auto i : strs){string value = i;sort(i.begin(), i.end());hash[i].push_back(value);}for(auto i : hash){ans.push_back(i.second);}return ans;}
};

http://www.dtcms.com/a/288382.html

相关文章:

  • WSL的功能及用途
  • Paimon 在维表 Lookup Join 场景中的应用
  • Spring AI - ChatClient接口演示
  • FMEA-CP-PFD三位一体数字化闭环:汽车部件质量管控的速效引擎
  • 11.1Redis高可用集群部署
  • Js中var VS let VS const
  • Jmeter系列(7)-线程组
  • 6.表内容的操作之增、读
  • 【Linux服务器】-mysql数据库数据目录迁移
  • 打靶日记之xss-labs
  • undefined reference to ‘end‘
  • 从公共数据到医学研究:32 个生信数据库详解
  • [实战]巴特沃斯滤波器全流程解析:从数学原理到硬件实现
  • 磁盘阵列技术的功能与分类
  • 【面试题】大厂高压面经实录丨第三期
  • Python Pandas读取Excel表格中数据并根据时间字段筛选数据
  • 软件警告弹窗与兼容性问题
  • HD Video Converter Factory pro 高清视频转换器 v27.7.0 绿色中文便携版
  • centos7使用docker-compose部署项目
  • 《Linux 环境下 NTP 时间同步与 SSH 免密登录配置实战》
  • 树的基础知识总结
  • 移动硬盘无法读取怎么办?数据还可以抢救回来
  • STC增强型单片机寄存器 PWM EEPROM TMOD TCON
  • 数据结构的文件操作
  • scalelsd 笔记 线段识别 本地部署 模型架构
  • Spring7个事务传播行为和5个隔离级别
  • Java-Lambda表达式
  • 【Linux】Linux中重定向 及 dup2 详细讲解
  • Stream API
  • 2023 年 5 月青少年软编等考 C 语言八级真题解析