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. ...
随机推荐
- DB2 数据库常用操作【持续更新】
好久没写博客了. 上次还是两个月前. 1. 连接数据库 db2 connect to dbName user userName using password 2. 查看表结构 db2 "de ...
- .net对Redis集群的调用(FrameWork版本4.0)
使用 StackExchange.Redis 实现.net对Redis集群的调用,方法如下: 1.打开 项目-管理NuGet程序包 2.在打开界面搜索StackExchange.Redis,找到Sta ...
- lepus天兔数据库监控
本篇文章的前提是服务器装了mysql服务.git,我这边就不写出来了,自行百度,装下mysql服务,比较简单 一.安装LAMP基础环境 Xampp下载地址:https://www.apachefrie ...
- Everyone is tester
有一本书叫<人人都是产品经理>,作者在书中介绍了在做产品的过程中学到的思维方式和做事方式,受到行业大众的认可 作为一名测试老鸟,我想说,其实Everyone is tester 为 ...
- 【BZOJ4774】修路(动态规划,斯坦纳树)
[BZOJ4774]修路(动态规划,斯坦纳树) 题面 BZOJ 题解 先讲怎么求解最小斯坦纳树. 先明白什么是斯坦纳树. 斯坦纳树可以认为是最小生成树的一般情况.最小生成树是把所有给定点都要加入到联通 ...
- AtCoder Grand Contest 004
AtCoder Grand Contest 004 A - Divide a Cuboid 翻译 给定一个\(A*B*C\)的立方体,现在要把它分成两个立方体,求出他们的最小体积差. 题解 如果有一条 ...
- bzoj1683[Usaco2005 Nov]City skyline 城市地平线
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格 ...
- 洛谷 P3235 [HNOI2014]江南乐 解题报告
P3235 [HNOI2014]江南乐 Description 两人进行 T 轮游戏,给定参数 F ,每轮给出 N 堆石子,先手和后手轮流选择石子数大于等于 F 的一堆,将其分成任意(大于1)堆,使得 ...
- mac 使用tree命令
下载软件包: http://mama.indstate.edu/users/ice/tree/ 安装说明: http://www.qiansw.com/mac-os-x-install-tree-co ...
- c动态分配结构体二维数组
这个问题我纠结了蛮久了,因为前面一直忙(自己也懒了点),所以没有能好好研究这个.希望这篇文章能够帮助你们. #include <stdio.h> #include <stdlib.h ...