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. ...
随机推荐
- 安装Zookeeper出现Unable to start AdminServer,existing abnormally问题解决方法
问题如下: 出现这个问题主要是由于8080端口占用,可在zoo.cfg中增加admin.serverPort=没有被占用的端口号解决问题.
- 当给属性添加final 时候 则无法进行第二次值的修改
- 基于c的简易计算器一
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h&g ...
- P1110 [ZJOI2007]报表统计
题目描述 Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一. 经过仔细观察,小Q发现统计一张报表实际上是维护一个非负整数数列,并 ...
- 【集训】练习题 uria
Description 求有多少组正整数对 \((a, b)\) 满足 \(a + b ≤ n\) \(a + b | ab\) \(n ≤ 10^14\) Solution 这题有点绕啊 设 \(g ...
- 【BootStrap】Table的基本使用
一.前言 新年新气象,转眼今年就28了,不知道今年能不能把妹成功呢?哈哈哈!上班第一天,部门Web技术主管给每个同事都发了红包鼓励大家今年加油,我作为新转入部门员工不能给团队掉链子,要加 ...
- 《Linux内核设计与实现》第18章读书笔记
第十八章 调试 一.调试开始前的准备 1.准备开始 bug 藏匿bug的版本 相关内核代码的知识 成功调试的关键在于能否将错误重现 2.内核中的bug 其产生原因无数,表象变化也多种多样.从隐藏在源代 ...
- spoj 375 树链剖分 模板
QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...
- Java入门:读写文本文件
文本文件的读写是学习java必须掌握的一项基本技术,因为在项目中时常会涉及到文本文件的读写. 一.使用FileWriter写文件 1.FileWriter类 [功能] FileWriter类专门用来写 ...
- Service Fabric —— Actor / Stateless Service 概念
作者:潘罡 (Van Pan) @ Microsoft 上一节我们谈到了Stateful Service.在Service Fabric中,Stateful Service是理解Micro Servi ...