【题目】:

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

The function twoSum should return indices of the two numbers such that they add up to the target, 
where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

【题意】:

  题目中要求输入一个整形数组以及一个target,找出该整型数组中这样两个元素,使得这两个元素之和等于指定target的值。 题目中假设该两个元素必然存在,并且是只有一组(所以相对简单),返回的是这两个元素的index值(数组Index从1开始)。

【坑】:

  我们拿到这个题目,首先能够想到的就是两次遍历,但是这样时间复杂度为O(n^2),无法通过leetCode的OJ测试。所以需要找其他方法。必然要小于O(n^2)复杂度才行。

【key point】:

  我们经常会使用空间换取时间的方法来获取较小的时间复杂度。因此增加必要的数据结构来减少时间复杂度。这道题目一样,我们增加一个map结构,key为数组元素值,value为其在数组中对应的index。每次遍历到数组元素便去map结构中查找对应的补数,如果存在,那就说明找到了。如果没存在就记录当前元素以及其index,直到结束。

【解答】:

 class Solution{
public:
// O(n) runtime, O(n) space
// We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.
std::vector<int> twoSum(std::vector<int>& numbers, int target){
std::vector<int> vecRet;
std::map<int, int> mapIndex;
for (size_t i = ; i < numbers.size(); ++i){
if ( != mapIndex.count(target - numbers[i])){
int nIndex = mapIndex[target - numbers[i]];
// 当前存储的Index肯定比i要小,注意要排除i
if (nIndex < i){
vecRet.push_back(nIndex + );
vecRet.push_back(i + );
return vecRet;
}
} else {
mapIndex[numbers[i]] = i;
}
}
return vecRet;
} // twoSum
 };

【leetCode Submission】

【运行结果】:

希望各位看官不吝赐教,小弟感恩言谢~

[leetCode][012] Two Sum (1)的更多相关文章

  1. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

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

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

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. js判断元素是否隐藏的方法

    代码如下: JavaScript代码如下: if( document.getElementById("div").css("display")==='none' ...

  2. samba服务搭建及管理

    关闭防火墙 # /etc/init.d/iptables stop # chkconfig --level iptables off 关闭SELINUX # vim /etc/sysconfig/se ...

  3. iOS __block类型变量作用域

    看下图 在c语言中,2个独立的函数是不可能互相访问局部变量的,但是__block提供了这个功能,它不单单能读变量,还可以对变量进行写!上图说明,block获得了i最后的真实值5,没有只取得0,这都是& ...

  4. CodeForces - 426B(对称图形)

    Sereja and Mirroring Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  5. [Android Pro] 监听内容提供者ContentProvider的数据变化

    转载自:http://blog.csdn.net/woshixuye/article/details/8281385 一.提出需求 有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者 ...

  6. July 19th, Week 30th Tuesday, 2016

    The good seaman is known in bad weather. 惊涛骇浪,方显英雄本色. You can't be afraid to fail. It's the only way ...

  7. SQLServer语句大使

    1.创建数据库create database 数据库名字 2.删除数据库drop database 数据库名字3.创建表(identity(1,1)自动增长,倍数为1,primary key设置主键) ...

  8. Struts2中过滤器和拦截器的区别

    拦截器和过滤器的区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而 ...

  9. .NET的堆和栈01,基本概念、值类型内存分配

    当我们对.NET Framework的一些基本面了解之后,实际上,还是很有必要了解一些更底层的知识.比如.NET Framework是如何进行内存管理的,是如何垃圾回收的......这样,我们才能写出 ...

  10. Java内存访问重排序笔记

    >>关于重排序 重排序通常是编译器或运行时环境为了优化程序性能而采取的对指令进行重新排序执行的一种手段. 重排序分为两类:编译期重排序和运行期重排序,分别对应编译时和运行时环境. > ...