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)的更多相关文章

  1. Golang高效实践之array、slice、map

    前言 Golang的slice类型为连续同类型数据提供了一个方便并且高效的实现方式.slice的实现是基于array,slice和map一样是类似于指针语义,传递slice和map并不涉及底层数据结构 ...

  2. 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 ...

  3. 【转载】Java集合类Array、List、Map区别和联系

    Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...

  4. 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 ...

  5. java 集合类Array、List、Map区别和优缺点

    Java集合类主要分为以下三类: 第一类:Array.Arrays 第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Array ...

  6. [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 ...

  7. Go语言学习笔记(四) [array、slice、map]

    日期:2014年7月22日   一.array[数组]   1.定义:array 由 [n]<type> 定义,n 标示 array 的长度,而 <type> 标示希望存储的内 ...

  8. 表示集合的数据结构:数组(Array),对象(Object),Map和Set

    Map和Set是ES6标准新增的数据类型 Map: 是一组键值对的结构,使用一个二维数组来初始化Map,例如: var m = new Map([['xiaohong',100],['xiaolan' ...

  9. JS内置对象-Array之forEach()、map()、every()、some()、filter()的用法

    简述forEach().map().every().some()和filter()的用法 在文章开头,先问大家一个问题: 在Javascript中,如何处理数组中的每一项数据? 有人可能会说,这还不简 ...

随机推荐

  1. seienium基础(测试脚本中的等待方法)

    测试脚本中的等待方法 一.加等待时间的目的 等待是为了使脚本执行更加稳定 二.常用的休眠方式 第一种  sleep(): 设置固定休眠时间.python 的 time 包提供了休眠方法 sleep() ...

  2. java_IO流(输入流)

    * 字节输入流(InputStream):所有字节输入流的父类 * 所有子类共性方法: * int read():从输入流中读取数据的下一个字节 * int read(byte[] b):从输入流中拂 ...

  3. Python---求100以内的质数

    1.首先什么是质数: 一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫质数,也叫素数.如2,3,5,7,11,13,17…. 2.代码如下: 这里做个解析:①Python的for ...

  4. 廖雪峰Java11多线程编程-3高级concurrent包-2ReadWriteLock

    ReentrantLock保证单一线程执行 ReentrantLock保证了只有一个线程可以执行临界区代码: 临界区代码:任何时候只有1个线程可以执行的代码块. 临界区指的是一个访问共用资源(例如:共 ...

  5. 集合划分——cf1028D思维题

    非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...

  6. this指针/常函数、常对象

    this指针引入 类中对象的成员变量和成员函数是分开存储的,sizeof(空class) = 1,另外示例中涉及到字节对齐的问题,double本身的字节为8,int为4,由于字节对齐,int也为8,所 ...

  7. mybatis接口映射

    通过sqlSession.getMapper();方法获取映射的接口及方法 sqlSession调用Configuration的getMapper方法,方法中使用了mapperRegistry.get ...

  8. IO流读取和写入文件

    package com.xmlmysql.demo.config; import java.io.BufferedReader; import java.io.BufferedWriter; impo ...

  9. swoole是如何实现任务定时自动化调度的?

    https://www.muzilong.cn/article/117 开发环境 环境:lnmp下进行试验. 框架:laravel5 问题描述 这几天做银行对帐接口时,踩了一个坑,具体需求大致描述一下 ...

  10. An invalid property 'jdbcType ' was found in mapping

    大概2种原因: 1 放进去的类型与字段的类型不匹配 2 比较变态,xml中=两边不能有空格! 错误示例如下:  #{plat,jdbcType = INTEGER}, 去掉空格后: #{plat,jd ...