【Leetcode】 1.two sum unordered_map 关联容器
来源:互联网 发布:软件ac管理控制器 编辑:程序博客网 时间:2024/05/16 01:50
001.Two Sum[E]
1.题目
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
实现:Hashmap
最后一种是比较聪明的做法,用hashmap,hashmap是内部存储方式为哈希表的map结构,哈希表可以达到查找O(1),哈希表的介绍可以看这里, C++实现Hashmap的方式,这里用unordered_map关联容器,可以实现键值对应。
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { //vector<int> result; unordered_map<int,int> mymap; int res; for(int i = 0;i < nums.size();i++) { res = target - nums[i]; unordered_map<int,int>::iterator it = mymap.find(res); if(it != mymap.end()) { return vector<int>({it->second,i}); } mymap[nums[i]] = i; } }};
1.介绍
最近使用到一个c++的容器——unordered_map,它是一个关联容器,内部采用的是hash表结构,拥有快速检索的功能。
1.1 特性
- 关联性:通过key去检索value,而不是通过绝对地址(和顺序容器不同)
- 无序性:使用hash表存储,内部无序
- Map : 每个值对应一个键值
- 键唯一性:不存在两个元素的键一样
- 动态内存管理:使用内存管理模型来动态管理所需要的内存空间
1.2 Hashtable和bucket
由于unordered_map内部采用的hashtable的数据结构存储,所以,每个特定的key会通过一些特定的哈希运算映射到一个特定的位置,我们知道,hashtable是可能存在冲突的(多个key通过计算映射到同一个位置),在同一个位置的元素会按顺序链在后面。所以把这个位置称为一个bucket是十分形象的(像桶子一样,可以装多个元素)。可以参考这篇介绍哈希表的文章
所以unordered_map内部其实是由很多哈希桶组成的,每个哈希桶中可能没有元素,也可能有多个元素。
2. 模版
- 1
- 2
- 3
- 4
- 5
- 6
主要使用的也是模板的前2个参数<键,值>(需要更多的介绍可以点击这里)
- 1
2.1 迭代器
unordered_map的迭代器是一个指针,指向这个元素,通过迭代器来取得它的值。
- 1
- 2
- 3
- 4
它的键值分别是迭代器的first和second属性。
- 1
- 2
3. 功能函数
3.1 构造函数
unordered_map的构造方式有几种:
- 构造空的容器
- 复制构造
- 范围构造
- 用数组构造
3.1.2示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
输出结果:
- 1
- 2
3.2 容量操作
3.2.1 size
- 1
- 2
返回unordered_map的大小
3.2.2 empty
- 1
- 2
- 为空返回true
- 不为空返回false,和用size() == 0判断一样。
3.3 元素操作
3.3.1 find
- 1
- 2
查找key所在的元素。
- 找到:返回元素的迭代器。通过迭代器的second属性获取值
- 没找到:返回unordered_map::end
3.3.2 insert
插入有几种方式:
- 复制插入(复制一个已有的pair的内容)
- 数组插入(直接插入一个二维数组)
- 范围插入(复制一个起始迭代器和终止迭代器中间的内容)
- 数组访问模式插入(和数组的[]操作很相似)
具体的例子可以看后面示例代码。
3.3.3 at
- 1
- 2
查找key所对应的值
- 如果存在:返回key对应的值,可以直接修改,和[]操作一样。
- 如果不存在:抛出 out_of_range 异常.
mymap.at(“Mars”) = 3396; //mymap[“Mars”] = 3396
3.3.4 erase
擦除元素也有几种方式:
通过位置(迭代器)
- 1
- 2
通过key
- 1
- 2
通过范围(两个迭代器)
- 1
- 2
3.3.5 clear
- 1
- 2
清空unordered_map
3.3.6 swap
- 1
- 2
交换两个unordered_map(注意,不是交换特定元素,是整个交换两个map中的所有元素)
3.3.7 示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
输出结果:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
3.4 迭代器和bucket操作
3.4.1 begin
- 1
- 2
- 3
- begin() : 返回开始的迭代器(和你的输入顺序没关系,因为它的无序的)
- begin(int n) : 返回n号bucket的第一个迭代器
3.4.2 end
- 1
- 2
- 3
- end(): 返回结束位置的迭代器
- end(int n) : 返回n号bucket的最后一个迭代器
3.4.3 bucket
- 1
- 2
返回通过哈希计算key所在的bucket(注意:这里仅仅做哈希计算确定bucket,并不保证key一定存在bucket中!)
3.4.4 bucket_count
- 1
- 2
返回bucket的总数
3.4.5 bucket_size
- 1
返回第i个bucket的大小(这个位置的桶子里有几个元素,注意:函数不会判断n是否在count范围内)
3.4.6 示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
输出结果:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
最后
unordered_map常用的功能函数介绍就这么多了,还有一些比较不常用的功能的介绍,可以参考这里
- 【Leetcode】 1.two sum unordered_map 关联容器
- [Leetcode] 1.Two Sum(unordered_map)
- 【leetcode】Two Sum(unordered_map的使用)
- leetcode--Two Sum--无序关联容器--hash map
- LeetCode 1 Two Sum(unordered_map和map的区别)
- LeetCode 599. Minimum Index Sum of Two Lists(map,unordered_map)
- 关联容器(五):unordered_map
- LeetCode 1. Two Sum
- [LeetCode]1.Two Sum
- LeetCode 1.Two Sum
- LeetCode --- 1. Two Sum
- [Leetcode] 1. Two Sum
- leetcode---1.Two sum
- [Leetcode] 1. Two Sum
- LeetCode 1.Two Sum
- LeetCode 1.Two Sum
- LeetCode 1.Two Sum
- 【LeetCode]1.Two Sum
- 【运维】浅识监控
- 解决跨域问题的8种方案(最新最全)
- Starting Tomcat v7.0 Server at localhost' has encountered a problem
- [Kaggle] Titanic: Machine Learning from Disaster入门版练习笔记
- [Mysql]——事务的4种隔离级别
- 【Leetcode】 1.two sum unordered_map 关联容器
- 微信小程序----侧滑菜单(Popup---左侧滑动,右侧不动)
- Java主函数
- apache 自带的ab.exe测试
- 【Codeforces776G】Sherlock and the Encrypted Data
- spark分布式集群环境搭建(hadoop之上)
- AOP详解,转载
- Java中读取jar包中的文件
- webpack