[Array]1. Two Sum(map和unorder_map)
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
思路:对于这种的不能进行排序,排序之后的元素下标就变的不一样了。 自己的代码:还是很低级,之前做过167了。。。。遍历实在浪费时间
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>index;
int n = nums.size();
for(int i = ; i < n-; i++){
for(int j = i+; j < n; j++){
if(nums[i] + nums[j] == target){
index.push_back(i);
index.push_back(j);
//return vector<int>{i,j};
}
}
}
return index;
}
优秀代码:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int>temp;
for(int i = ; i < nums.size(); i++){
int sub = target = nums[i];
if(temp.find(sub) != nums.end()){
return vector<int>{temp[sub], i};
else
temp[nums[i]] = i; //这里将元素值和元素下标对应
}
}
}
unordered_map用法
[查找元素是否存在]
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
若有unordered_map<int, int> mp;查找x是否在map中
方法1: 若存在 mp.find(x)!=mp.end()
方法2: 若存在 mp.count(x)!=0
[插入数据]
map.insert(Map::value_type(1,"Raoul"));
[遍历map]
unordered_map<key,T>::iterator it;
(*it).first; //the key value
(*it).second //the mapped value
for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;
map用法
map经常用于c++中的二维数组,虽然我以前一直用vector<int, vector<int> >,还是要学着用一下map
参考:http://blog.csdn.net/u012530451/article/details/53228098; http://blog.csdn.net/bzhxuexi/article/details/24182087
[Array]1. Two Sum(map和unorder_map)的更多相关文章
- Golang高效实践之array、slice、map
前言 Golang的slice类型为连续同类型数据提供了一个方便并且高效的实现方式.slice的实现是基于array,slice和map一样是类似于指针语义,传递slice和map并不涉及底层数据结构 ...
- A. Array with Odd Sum Round #617(水题)
A. Array with Odd Sum time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- java 集合类Array、List、Map区别和优缺点
Java集合类主要分为以下三类: 第一类:Array.Arrays 第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Array ...
- [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...
- Go语言学习笔记(四) [array、slice、map]
日期:2014年7月22日 一.array[数组] 1.定义:array 由 [n]<type> 定义,n 标示 array 的长度,而 <type> 标示希望存储的内 ...
- 表示集合的数据结构:数组(Array),对象(Object),Map和Set
Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...
- JS内置对象-Array之forEach()、map()、every()、some()、filter()的用法
简述forEach().map().every().some()和filter()的用法 在文章开头,先问大家一个问题: 在Javascript中,如何处理数组中的每一项数据? 有人可能会说,这还不简 ...
随机推荐
- wpf 让正执行的程序暂停几秒钟
public static class DispatcherHelper { [SecurityPermissionAttribute(SecurityAction.Deman ...
- netty DelimiterBasedFrameDecoder
netty server EchoServer package com.zhaowb.netty.ch5_1; import io.netty.bootstrap.ServerBootstrap; i ...
- linux nload命令简介及安装方法
第一步:nload命令简介 nload 命令用于查看linux网络流量状况,实时输出.可以理解为是一个控制台应用程序,用来实时监测网络流量和带宽使用情况的命令. 使用可视化的界面显示流入和留出系统的网 ...
- spring基于xml的IOC环境搭建和入门
配置pom.xml的依赖 <packaging>jar</packaging> <dependencies> <dependency> <grou ...
- 初探Remax微信小程序
1.创建项目 npx degit remaxjs/template-wechat my-app cd my-app && npm install 2.运行项目 npm run dev ...
- 廖雪峰Java11多线程编程-3高级concurrent包-4Concurrent集合
Concurrent 用ReentrantLock+Condition实现Blocking Queue. Blocking Queue:当一个线程调用getTask()时,该方法内部可能让给线程进入等 ...
- Linux的基本原则
Linux的基本原则:1.由目的单一的小程序组成,一个程序只做一件事,且做好: 2.’组合小程序完成复杂任务: 3.一切皆文件: 4.尽量避免捕获用户接口: 5.配置文件保存为纯文本格式: 6.提供机 ...
- day 45 前端CSS
前端CSS CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素,给HTML设置样式,让它更加美观. 当浏览器读到一个样式表,它就会按照这个样式 ...
- java基础之Math类
Math类概述Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 成员方法 public static int abs(int a):绝对值 public static ...
- Luogu P2101 命运石之门的选择(分治+搜索)
P2101 命运石之门的选择 题意 题目描述 在某一条不知名世界线的冈伦今天突然接到了一条\(dmail\),上面说世界线将会发生巨大变动,未来的他无论如何都无法扭转这种变动回到原来的世界线.而世界线 ...