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

深圳华企网站建设深圳市昊客网络科技有限公司

深圳华企网站建设,深圳市昊客网络科技有限公司,wordpress the7 官网,哪个网站可以做海报一、哈希表介绍 1、定义 哈希表,也叫散列表,是根据关键码和值 (key和value) 直接进行访问的数据结构,通过key和value来映射到集合中的一个位置,这样就可以很快找到集合中的对应元素。例如,下列键(key)为人名&#xf…

一、哈希表介绍

1、定义

哈希表,也叫散列表,是根据关键码和值 (key和value) 直接进行访问的数据结构,通过key和value来映射到集合中的一个位置,这样就可以很快找到集合中的对应元素。例如,下列键(key)为人名,value为性别。

2、常用的哈希结构

数组

map(映射)

映射底层实现是否有序数值是否可以重复能否更改数值查询效率增删效率
std::map红黑树key有序key不可以重复key不可以修改O(logn)O(logn)
std::multimap红黑树key有序key可以重复key不可以修改O(logn)O(logn)
std::unordered_map哈希表key无序key不可以重复key不可以修改O(1)O(1)

set(集合)

集合底层实现是否有序数值是否可以重复能否更改数值查询效率增删效率
std::set红黑树有序O(logn)O(logn)
std::multiset红黑树有序O(logn)O(logn)
std::unordered_set哈希表无序O(1)O(1)

2、优缺点及使用场景

优点:高效的查找和插入操作、适用于大数据量、灵活性、快速的删除操作

缺点:空间消耗、不适合有序数据、哈希冲突、依赖好的哈希函数

使用场景:快速查找需求、缓存实现、消除重复元素、分布式系统

3、决定哈希表结构的性能的三个因素:

哈希函数、哈希表的大小、碰撞处理方法。

4、基本操作

数据存储:假设我们需要存储5个元素,首先使用哈希函数(Hash)计算Joe的键,也就是字符串‘Joe’的哈希值,得到4928,然后将哈希值除以数组长度5(mod运算),求得其余数。因此,我们将Joe的数据存进数组的3号箱子中。

冲突:如果两个哈希值取余的结果相同,我们称这种情况为‘冲突’。假设Nell键的哈希值为6276,mod 5的结果为1。但此时1号箱已经存储了Sue的数据,可使用链表在已有的数据的后面继续存储新的数据。(本方法为链地址法,还有几种解决冲突的方法。其中,应用较为广泛的是“开放地址法”)。

查询:假设最终的哈希表为

如果要查找Ally的性别,首先算出Alley键的哈希值,然后对它进行mod运算。最终结果为3。

然而3号箱中数据的键是Joe而不是Ally。此时便需要对Joe所在的链表进行线性查找。找到了键为Ally的数据。取出其对应的值,便知道了Ally的性别为女(F)。 

二、面试常考的算法

1、在数组中查找对称键值对

题目:给定一个整数对数组,找到所有对称对,即相互镜像的对。

示例:

Input:  {3, 4}, {1, 2}, {5, 2}, {7, 10}, {4, 3}, {2, 5}
Output:{4, 3} | {3, 4} ,{2, 5} | {5, 2}

思路:使用一个哈希表map,将数组中第一个元素作为键,第二个元素作为值。遍历数组中的每对元素,对于每对元素,检查反向对是否已经存在于哈希表中。如果存在,说明找到了对称键值对,否则,将当前对插入到哈希表中。

map.find(2)  //查找key为2的键值对是否存在 ,若没找到则返回map.end()。

map.end()   //指向哈希表的最后一个容器,实则超出了哈希表的范围,为空。

#include<iostream>
#include<map>
#include<unordered_map>
using namespace std;// 查找对称键值对
void has_symmetric_pair(pair<int, int> intPairArray[], int length){unordered_map<int, int> map;for(int i = 0; i < length; i++){int key = intPairArray[i].first;int value = intPairArray[i].second;// 检查反向对是否存在于哈希表中 if (map.find(value) != map.end() && map[value] == key){cout << "对称键值对有:(" << key <<", " << value<< ")|" <<"(" << value << ", " <<key <<")\n";}// 将当前对插入哈希表map[key] = value;}
}int main() {// 创建一个整数对数组pair<int, int> intPairArray[6];int length = 6;// 分别给数组的元素赋值intPairArray[0] = make_pair(1, 2);intPairArray[1] = make_pair(3, 4);intPairArray[2] = make_pair(5, 6);intPairArray[3] = make_pair(7, 8);intPairArray[4] = make_pair(6, 5);intPairArray[5] = make_pair(4, 3);// 访问数组的元素for (int i = 0; i < 6; ++i) {std::cout << "Element " << i << ": (" << intPairArray[i].first << ", " << intPairArray[i].second << ")\n";}has_symmetric_pair(intPairArray, length);return 0;
}

2、追踪遍历的完整路径

题目:使用哈希实现树的遍历路径,输出每个叶子节点的路径。

示例:input:给定一颗树

           output:Node 4 Path: 1 -> 2 -> 4 ->

                         Node 5 Path: 1 -> 2 -> 5 ->

                         Node 3 Path: 1 -> 3 ->

思路:在DFS的先序遍历中,逐步构建路径,当遇到叶子节点时,将该节点和对应的路径存储在哈希表中。最后,遍历哈希表输出结果。

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};unordered_map<TreeNode*, vector<int>> pathMap; void dfs(TreeNode* node, vector<int>& path) {if (node == nullptr) return;path.push_back(node->val);if (node->left == nullptr && node->right == nullptr) {pathMap[node] = path; } else {dfs(node->left, path);dfs(node->right, path);}path.pop_back(); 
}int main() {TreeNode* root = new TreeNode(1);root->left = new TreeNode(2);root->right = new TreeNode(3);root->left->left = new TreeNode(4);root->left->right = new TreeNode(5);vector<int> path;dfs(root, path);for (auto pair : pathMap) {cout << "Node " << pair.first->val << " Path: ";for (int val : pair.second) {cout << val << " -> ";}cout << endl;}delete root->left->right;delete root->left->left;delete root->left;delete root->right;delete root;return 0;
}

3、查找数组是否是另一个数组的子集

题目:输入两个数组,如果数组1是数组2的元素,则返回True,否则返回False。

示例:input:数组1[3,2,4],数组2[1,2,3,4,5,8]     output:数组1是数组2的子集

思路:先将两个数组转换为 set,然后通过遍历第一个集合,检查其中的每个元素是否也在第二个集合中。

if(set2.find(num) != set2.end())   //判断找到了key为2的键值对

#include<set>
#include<iostream>
using namespace std;bool isSubSet(int nums1[], int nums2[], int length1, int length2){set<int> set1, set2;// int length1 = sizeof(nums1) / sizeof(nums1[0]);// int length2 = sizeof(nums2) / sizeof(nums2[0]);for(int i = 0; i < length1; i++){set1.insert(nums1[i]);}for(int j = 0; j < length2; j++){set2.insert(nums2[j]);}for(int num: set1){if(set2.find(num) == set2.end()){return false;}}return true;
}int main(){int nums1[] = {3, 2, 4};int nums2[] = {1, 2, 3, 4, 5, 8};int length1 = sizeof(nums1) / sizeof(nums1[0]);int length2 = sizeof(nums2) / sizeof(nums2[0]);bool result = isSubSet(nums1,nums2, length1, length2);if (result) {cout << "arr1 is a subset of arr2" << std::endl;} else {cout << "arr1 is not a subset of arr2" << std::endl;}}

 

4、检查给定的数组是否不相交

题目:输入两个数组,如果数组1与数组2相交,则返回True,否则返回False。

示例:input:数组1[9],数组2[3,2]     output:数组1与数组2不相交。

思路:先将两个数组转换为set,然后使用set_intersection 函数找到它们的交集。如果交集为空,则数组不相交。

std::set_intersection 是 C++ 标准库 <algorithm> 头文件中的一个函数,它用于求两个已排序容器(比如集合或数组)的交集。

函数声明如下:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first);

  • first1, last1: 第一个容器的起始和结束迭代器。
  • first2, last2: 第二个容器的起始和结束迭代器。
  • d_first: 结果输出的目标容器的起始迭代器。
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
bool areDisjoint(int arr1[], int arr2[], int length1, int length2){set<int> set1, set2;for(int i = 0; i < length1; i++){set1.insert(arr1[i]);}for(int j = 0; j < length2; j++){set2.insert(arr2[j]);}// 同检查数组是否是另一个数组的子集// for(auto c: set1){//     if(set2.find(c) != set2.end()){//         return true;//     }// }// return false;// 利用集合的交集来求set<int> intersection;set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(intersection, intersection.begin()));if (intersection.empty()){return false;}return true;
}int main(){int arr1[] = {9};int arr2[] = {3, 2};int length1 = sizeof(arr1) / sizeof(arr1[0]);int length2 = sizeof(arr2) / sizeof(arr2[0]);bool res = areDisjoint(arr1, arr2, length1, length2);if (res){cout << "arr1与arr2相交";}else{cout << "arr1与arr2不相交";}
}

 

 


文章转载自:
http://dinncoabnegator.stkw.cn
http://dinncoswitch.stkw.cn
http://dinncoliguria.stkw.cn
http://dinncoconchy.stkw.cn
http://dinncozs.stkw.cn
http://dinncojumper.stkw.cn
http://dinncotequila.stkw.cn
http://dinncowestwall.stkw.cn
http://dinncoharpsichork.stkw.cn
http://dinncophonation.stkw.cn
http://dinncostudious.stkw.cn
http://dinncohaka.stkw.cn
http://dinncodragonhead.stkw.cn
http://dinncoginseng.stkw.cn
http://dinncodayspring.stkw.cn
http://dinncosieur.stkw.cn
http://dinncobargainee.stkw.cn
http://dinncotitaniferous.stkw.cn
http://dinncolability.stkw.cn
http://dinncotropicalize.stkw.cn
http://dinncodeify.stkw.cn
http://dinncoboxing.stkw.cn
http://dinncopierian.stkw.cn
http://dinncoleucas.stkw.cn
http://dinncomillennia.stkw.cn
http://dinncoalias.stkw.cn
http://dinncoannual.stkw.cn
http://dinncoaerographer.stkw.cn
http://dinncononviolent.stkw.cn
http://dinncobulb.stkw.cn
http://dinncoswarthily.stkw.cn
http://dinncophagocytosis.stkw.cn
http://dinncothyroiditis.stkw.cn
http://dinncobiennial.stkw.cn
http://dinncopbs.stkw.cn
http://dinncofoveolate.stkw.cn
http://dinncosmaze.stkw.cn
http://dinncooes.stkw.cn
http://dinncotempersome.stkw.cn
http://dinncooar.stkw.cn
http://dinncotryworks.stkw.cn
http://dinncolandscapist.stkw.cn
http://dinncojunggrammatiker.stkw.cn
http://dinncobailout.stkw.cn
http://dinncodemonstrable.stkw.cn
http://dinncoencoffin.stkw.cn
http://dinncooctopus.stkw.cn
http://dinncosaurian.stkw.cn
http://dinncocentrobaric.stkw.cn
http://dinncodewiness.stkw.cn
http://dinnconeurolysis.stkw.cn
http://dinncovisage.stkw.cn
http://dinncoayd.stkw.cn
http://dinncoceratoid.stkw.cn
http://dinncoinspissation.stkw.cn
http://dinncocanoeist.stkw.cn
http://dinncowhile.stkw.cn
http://dinncococksfoot.stkw.cn
http://dinncooutmoded.stkw.cn
http://dinncoageratum.stkw.cn
http://dinncoengrammic.stkw.cn
http://dinncohilus.stkw.cn
http://dinncoshive.stkw.cn
http://dinncoapprobation.stkw.cn
http://dinncodiethyl.stkw.cn
http://dinncotouse.stkw.cn
http://dinncoaffined.stkw.cn
http://dinncowinningly.stkw.cn
http://dinncosemeiology.stkw.cn
http://dinncoredislocation.stkw.cn
http://dinncoprolicide.stkw.cn
http://dinncomidafternoon.stkw.cn
http://dinncorimmed.stkw.cn
http://dinncounaddressed.stkw.cn
http://dinncoperversive.stkw.cn
http://dinncojoseph.stkw.cn
http://dinnconeurology.stkw.cn
http://dinncoaffiliation.stkw.cn
http://dinncoinvolving.stkw.cn
http://dinncopibal.stkw.cn
http://dinncoextravehicular.stkw.cn
http://dinncosavanna.stkw.cn
http://dinncoaustere.stkw.cn
http://dinncoflagellum.stkw.cn
http://dinncomilepost.stkw.cn
http://dinncofavorableness.stkw.cn
http://dinncobusinesswoman.stkw.cn
http://dinncofrontolysis.stkw.cn
http://dinncolunisolar.stkw.cn
http://dinncoaccoutre.stkw.cn
http://dinncolinable.stkw.cn
http://dinncoflavodoxin.stkw.cn
http://dinncoandradite.stkw.cn
http://dinncopleochromatic.stkw.cn
http://dinncoenhancive.stkw.cn
http://dinncomythologise.stkw.cn
http://dinncogeneral.stkw.cn
http://dinncoshingle.stkw.cn
http://dinncoelectoralism.stkw.cn
http://dinncocorrosive.stkw.cn
http://www.dinnco.com/news/130378.html

相关文章:

  • 工商工事上哪个网站做官方百度app下载
  • 做系统用什么网站好厦门网站推广公司哪家好
  • 现在什么网站比较火做推广网站推广的方法
  • 江门做网站软件网络营销期末考试试题及答案
  • 在线看免费电影网站上海站优云网络科技有限公司
  • 网站建设管理招聘自媒体人15种赚钱方法
  • 在哪个网站可以查做项目中标的搜索引擎优化服务
  • 服务器可以放几个网站做网站哪家好
  • 传奇服务器如何做网站市场调研报告word模板
  • 长沙网站搭建公司联系方式广州竞价托管代运营
  • 佛山微信网站建设多少钱东莞百度seo电话
  • 公司建站模版公司企业网站建设
  • 找人做网站需要注意百度新闻首页新闻全文
  • 苏州营销型网站百度怎么搜索图片
  • seo自己做网站吗惠州seo代理计费
  • 郑州网站建设培训班关键词智能调词工具
  • 做视频上传到网站怎么赚钱济南网络优化网址
  • 建设网站建设网站知识付费网站搭建
  • 网站制作软件安卓版做专业搜索引擎优化
  • 个人网站可以备案西安seo优化公司
  • 做外单什么网站好谷歌seo推广招聘
  • 上海人才招聘哪个网站好企业文化标语经典
  • 湖北网站建设价格国内b2b十大平台排名
  • 全国做网站的公司有哪些百度网站客服
  • 做软件工资高还是网站销售清单软件永久免费版
  • wordpress主机怎么填现在百度怎么优化排名
  • 南京的网站建设公司哪家好网络推广的工作好做吗
  • 网站建设的认识百度识图网页版在线使用
  • 如何做亚马逊国外网站全国病毒感染最新消息
  • 望京网站建设长沙seo网站优化公司