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.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.

思路是循环一次,每次都判断当前数组索引位置的值在不在hashtable里,不在的话,加入进去,key为数值,value为它的索引值;在的话,取得他的key,记为n(此时n一定小于循环变量i),接下来再在hashtable中查找(target-当前数值)这个数,利用了hashtable中查找元素时间为常数的优势,如果找到了就结束,此处需要注意的是,如果数组中有重复的值出现,那么第二次出现时就不会加入到hashtable里了,比如3,4,3,6;target=6时,当循环到第二个3时,也可以得到正确结果。
        最终ac的代码如下:
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int > res;
map<int,int> numMap;
map<int,int>::iterator iter;
int len=numbers.size();
for(int i=;i<len;i++)
{
iter=numMap.find(target-numbers[i]);
if(iter!=numMap.end())
{
res.push_back(iter->second);
res.push_back(i);
return res;
}
else
{
numMap[numbers[i]]=i;
}
} }
};

Two Sum ——经典的哈希表的题的更多相关文章

  1. UVa 1152 -4 Values whose Sum is 0—[哈希表实现]

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  2. POJ 3349 Snowflake Snow Snowflakes (哈希表)

    题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...

  3. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  4. 1. Two Sum (快速排序;有序数组的查找: 两个指针; 哈希表)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  7. Leetcode No.1 Two Sum(c++哈希表实现)

    1. 题目 1.1 英文题目 Given an array of integers nums and an integer target, return indices of the two numb ...

  8. 哈希表之bkdrhash算法解析及扩展

    BKDRHASH是一种字符哈希算法,像BKDRHash,APHash.DJBHash,JSHash,RSHash.SDBMHash.PJWHash.ELFHash等等,这些都是比較经典的,通过http ...

  9. 15 BasicHashTable基本哈希表类(二)——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...

随机推荐

  1. mybatis的Mapper代理原理

    前言:在mybatis的使用中,我们会习惯采用XXMapper.java+XXMapper.xml(两个文件的名字必须保持一致)的模式来开发dao层,那么问题来了,在XXMapper的文件里只有接口, ...

  2. ACE线程管理机制-并发控制(1)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581771.html ACE Lock类属 锁类属包含的类包装简单的锁定机制,比如互斥体 ...

  3. Maatkit--Mysql的高级管理工具

    Maatkit是不错的mysql管理工具,已经成为Percona的一部分.包含以下主要工具: 1.mk-table-checksum 检查主从表是否一致的有效工具 2.mk-table-sync 有效 ...

  4. git 回退

    回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到前3次提交之前,以此类推,回退到n次提交之前 $ git rese ...

  5. (转)Django发送html邮件

    本文转自http://blog.csdn.net/yima1006/article/details/8991145 send_mail(subject, message, from_email, re ...

  6. 「6月雅礼集训 2017 Day7」电报

    [题目大意] 有n个岛屿,第i个岛屿有有向发射站到第$p_i$个岛屿,改变到任意其他岛屿需要花费$c_i$的代价,求使得所有岛屿直接或间接联通的最小代价. $1 \leq n \leq 10^5, 1 ...

  7. codevs1066&&noip引水入城

    这道题 解决第一问 用灌水法 枚举第一行的每一个点 查找是否最后一行的每一个点是否都能灌到水 第二问 用反灌水发 枚举最后一行的每一个点 解决第一行每一个点所能覆盖的左右端点 可以证明每个点所能覆盖的 ...

  8. Java多线程学习(三)volatile关键字

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79680693 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  9. AlertDialog.Builder 显示为白色 蓝色字

    AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity(),AlertDialog.THEME_HOLO_LIGHT);

  10. 81.Search in Rotated Sorted Array II---二分变形

    题目链接 题目大意:与33题类似,只是这里数组中有重复数值. 法一:解法与33题类似,只是这里要处理1,3,1,1,1这种情况,即有重复值时,mid与left和right都相等时,可以采用right- ...