2016.07.13-map的使用(以leetcode1-Two Sum为例)
map的使用
1.unordered_map和map的区别
2.如何用
3.for (int a : nums1)
4.to_string()
5.map的应用
1.unordered_map和map的区别
相同点:
map和unordered_map都是<key,value>的形式,这是固定的,对于map中的每一组映射,first都是key均为键、second均为value值.
通过key快速索引到value,最后返回的是value。<key,value>可以是<int,int>,<char,int>,<string,int>等等。
可以统计字母出现的次数,单词出现的次数,那么key就是字母,单词,value就是每次队员的单词字母出现的个数
无论是map.find(key),map.count(key),map.ereas(key),map.insert(key),均是对key操作,同样返回的map[key]即为value
区别:
map的头文件为"map",multimap的头文件也是“map”,unordered_map头文件则是"unodered_map"。
map中会自动根据key值进行排序(按照二叉搜索树存储,map中自建了一颗红黑树,根据key的准则自动排序),而unodered_map则不排序为乱序。所以unodered_map的效率要高于map,multimap允许键重复出现
在map中使用make_pair(x,y)将x,y看做一组映射,x为Key,y为value
m.count(key):由于map不包含重复的key,因此m.count(key)取值为0,或者1,表示是否包含。
2.unordered_map和map的使用
使用:
以leetcode1-Two Sum为例
1.新建map时,map类型<key值类型,value值类型> 函数名
2.map中和数组一样用的是方括号“[ ]”
将vector<int>中或者数组中的元素添加到map中时:map[nums[i]] = i; 这里是用下标作为key,用下标的值对应value。
3.map.find(需要find的值) 如果找到则返回此元素的迭代器,若没有找到则返回end()的迭代器(即查找到尾部都没有找到)
所以用map.find(number) != map.end()表示找到元素
用map.find(number) == map.end()表示没有找到元素
#include "stdafx.h"
#include "iostream"
#include "unordered_map" //unodered_map的头文件
#include "vector"
using namespace std; class MyClass
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> res;
unordered_map<int, int> findNum; //初始化名为hash的hash table,<key,value>均为int型
int size = nums.size();
for (int i = ; i < size; i++)
{
int numToFind = target - nums[i];
if (findNum.find(numToFind) != findNum.end())
{
res.push_back(findNum[numToFind]); //map中和数组一样用的是[ ]
res.push_back(i); //push_back()是vector中的,不是map中的
return res; //只能输出一组,得到后直接跳出程序,返回值
}
findNum[nums[i]] = i; //由此可以看到下标为键(key),下标对应的值为值(value)
}
return res;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { , , , , , , , , , };
int target = ;
vector<int> res;
MyClass solution;
res = solution.twoSum(nums, target);
int size = res.size();
for (int i = ; i < size; i++)
{
cout << res[i] << " ";
}
cout << endl;
system("pause");
return ;
}
3.for (int a : nums1)
for (int a : nums1)和for(int i = 0;i<size;i++)
for(int i = 0;i<size;i++)是进行size次循环,每次改变i的值,比如可以比较nums[i]和nums[i+1]的值,也可以改变nums[i]的值,这里与i有关。
for (int a : nums1)则是直接把nums1这个数组或者vector中的值赋值给a,直到nums值全部执行完
vector<int> nums1 = { , , , };
int len = nums1.size();
unordered_map<int, int>nums;
for (int a : nums1) nums[a];
for (int i = ; i < len; i++) nums[nums1[i]];
上述均是把nums1中的值添加到nums这个map中
4.to_string()
to_string()
如果你需要的整型数的操作,但是最后返回的却是string型,那么可以用to_string(x),将整型x变成string型
to_string(countA) + 'A' + to_string(countB) + 'B';
5.map的应用
map中应用
如205. Isomorphic Strings同构字符串(paper,title) ,290. Word Pattern(abba,对应dog,cat,cat,dog)
构建2个map,使其为映射关系,在根据第二map查看映射是否正确
如217. Contains Duplicate,242. Valid Anagram,218. Contains Duplicate II,299. Bulls and Cows
则根据vector或是string中规律,建一个map,根据key和value的情况来做。
2016.07.13-map的使用(以leetcode1-Two Sum为例)的更多相关文章
- http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html
http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html T ...
- 多线程博文地址 http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html
http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html
- 2016/07/11 PHP接口的介绍与实现
接口定义了实现某种服务的一般规范,声明了所需的函数和常量,但不指定如何实现.之所以不给出实现的细节,是因为不同的实体可能需要用不同的方式来实现公共的方法定义.关键是要建立必须实现的一组一般原则 ...
- 2016.08.13/2/index/_d_Lucene54_0.dvm: Too many open files
er[file_system_exception: /elk/elasticsearch/data/es_cluster/nodes/0/indices/logstash-zjzc-frontend- ...
- 2016.07.13-vector<vector<int>>应用2——Two Sum扩展
收获: vector<vector<int> >res,不能直接用res[j].push_back(number),因为res[j]是空的,没有初始化 可以先定义 vector ...
- 2016.2.13 (年初六) oracle两张表update方法
A表customers和B表tmp_cust_city有3个相同字段, customer_id,city_name,customer_type 现要根据b表更新a表 更新一个字段情况: update ...
- Murano Weekly Meeting 2016.07.26
Meeting time: 2016.July.26 1:00~2:00 Chairperson: Nikolay_St, from Mirantis Meeting summary: 1.Masc ...
- Murano Weekly Meeting 2016.07.19
Meeting time: 2016.July.19 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- Murano Weekly Meeting 2016.07.12
Meeting time: 2016.July.12 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
随机推荐
- Qt4问题集锦
一.Qt程序发布后加载的jpg.jpeg图片不可见 问题的提出: 最近在客户机器上部署安装QT编写的软件,发现只要是jpg.jpeg格式的图片都无法显示出来.最后发现必须按如下述步骤才能显示jpg.j ...
- 小菜菜mysql练习50题解析——数据准备
附上数据准备: 学生表 create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10)); ...
- 【hihocoder编程练习赛9】闰秒
题目链接 #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h&g ...
- Linux中的防火墙----iptables
防火墙,它是一种位于内部网络与外部网络之间的网络安全系统.一项信息安全的防护系统,依照特定的规则,允许或是限制传输的数据通过. 防火墙根据主要的功能可分为网络层防火墙.应用层防火墙.数据库防火墙. 网 ...
- bug -- android 7.0 popwindow显示位置异常情况解决
android 7.0 popwindow显示位置异常,在android7.1官方进行解决了,但是还是要多7.0的bug进行解决,我的解决方案里面通过重写popwindow进行适配: import a ...
- 解题:POI 2008 Subdivision of Kingdom
题面 还可以这么搜......学到了(PoPoQQQ orz) 我们最朴素的做法是枚举所有状态(当然可以剪,剪完最终实际状态量也是$C_{26}^{13}$的),然后每次$O(n)$扫一遍判断,大概会 ...
- RF parameter
There are primarily 3 features which can be tuned to improve the predictive power of the model : 说明: ...
- Java入门:基础算法之产生随机数
本程序演示使用Random类的呢想tInt()方法产生随机数. /* Program: 随机数发生器 * Written by: 理工云课堂 * Input: None * Output: 0 到20 ...
- Docker图形界面管理之Portainer
介绍 Portainer是一个开源.轻量级Docker管理用户界面,基于Docker API,可管理Docker主机或Swarm集群,支持最新版Docker和Swarm模式.官方文档 https:// ...
- oracle改进之将阿拉伯数字转换成中文数字
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处 http://www.cnblogs.com/king-xg/p/6839738.html 将阿拉伯数字转 ...