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. 显示和隐藏(display属性)none或block

    显示和隐藏(display属性) 网页中经常会看到显示和隐藏的效果,可通过display属性来设置. 语法: Object.style.display = value 注意:Object是获取的元素对 ...

  2. [Ceoi2016|BZOJ4936] Match

    哈希+分治+stack 题目: 给你一个由小写字母组成的字符串s,要你构造一个字典序最小的(认为左括号的字典序比右括号小)合法的括号 序列与这个字符串匹配,字符串和括号序列匹配定义为:首先长度必须相等 ...

  3. JavaScript中定义函数的几种方式

    函数的组成:函数名 + 函数体 1.使用function关键字定义函数 -- 具有优先级,优先将function关键字定义的函数优先执行 function  functionName(arg0, ar ...

  4. 180608发现的一个有趣的Douyin-Bot项目

    今日发现的github 项目 Douyin-Bot 抖音机器人发现漂亮小姐姐 :sweat_smile: 在开发者模式下,找到指针位置,开启之后,当点击屏幕时,可以获取点击屏幕处的xy坐标了 我使用的 ...

  5. Vim操作 -- 按列

    1, 拷贝行 Y y 拷贝当前光标字符 如果需要拷贝整个单词,可以用 ye,e表示跳到词尾 2, 粘贴 P(大写) 粘贴到光标前 p(小写)粘贴到光标后 3, 进入快操作模式 ctrl+q 4, 用 ...

  6. Nginx 和 Gunicorn 部署 Django项目

    目录 Nginx 和 Gunicorn 部署 Django项目 配置Nginx 安装配置Gunicorn 通过命令行直接启动 Gunicorn 与 uwsgi 的区别,用哪个好呢 Gunicorn u ...

  7. svn both sides of the move must be committed together

    从一个模块移动文件到另一个模块下,提交的时候报错. 选择2个模块工程,team-与资源库同步 选择2个模块下,2份文件,一份减号图标,一份加号图标,同时提交. 还有种情况,当你重名文件后,提交报错 打 ...

  8. 0617Python-介绍、三种运行方式、变量、标识符和关键字、获取属性

    一.什么是自动化测试? 1.定义 自动:让机器自己动,就是自动 自动化:让机器按照人类的要求,把软件的所有功能遍历一遍 2.传统测试和自动化测试的区别 传统测试:繁琐.机械.门槛低.工资低 自动化测试 ...

  9. vue-cli的使用指南

    vue-cli 2.0 安装vue-cli npm install -g vue-cli 创建一个项目模板 vue init <template-name> <project-nam ...

  10. history配置

    /etc/profile配置文件中,末尾增加如下参数项: HFILE=`who -m | awk '{print $1}'`readonly HISTFILE=/var/history/$HFILE- ...