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

给一个vector,一个value,要求求出vector之内的两数相加之和等于value的两个index。

这里的基本思想是用一个map先来保存vector元素的值与他们对应的index,然后循环vector,用value减去每个数之后,把差值直接放到map里面去寻找
看能不能找到对应的index,大体上就是这个思想,下面详见代码:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> index;
vector<int> result;
int sz = nums.size();
for (int i = ; i < sz; ++i){
index[nums[i]] = i;
}
map<int, int>::iterator it;
for (int i = ; i < sz; ++i){
if ((it = index.find(target - nums[i])) != index.end()){
if (it->second == i) continue;//这一步要注意,防止找到的index与当前的i是相等的
result.push_back(i + );
result.push_back(it->second + );
break;
}
}
return result;
}
};

附上java版本的代码,方法比上面简洁一点。不过道理还是基本一样的:

 public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int [] result = new int[2];
for(int i = 0; i < nums.length; ++i){
if(m.containsKey(nums[i])){
int index = m.get(nums[i]);
result[0] = index + 1;
result[1] = i + 1;
break;
}else{
m.put(target-nums[i] ,i);//很关键!
}
}
return result;
}
}

LeetCode OJ:Two Sum(两数之和)的更多相关文章

  1. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  2. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  3. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  4. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  5. [leetcode]1. Two Sum两数之和

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

  6. [LeetCode]1.Two Sum 两数之和(Java)

    原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...

  7. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  8. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  9. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  10. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. python删除目录下七天前创建的文件

    #coding=utf-8 import os import time import datetime def deleteOutdateFiles(path): """ ...

  2. 在python列表中删除所有空元素

    今天在测试数据的时候偶然发现一个问题,如下: test = ['a','','b','','c','',''] for i in test: if i == '': test.remove(i) pr ...

  3. Python中的lambda、map和filter

    翻译.修改自https://medium.com/@happymishra66/lambda-map-and-filter-in-python-4935f248593 1.lambda lambda运 ...

  4. jmeter 非GUI模式下测试报错An error occurred: Unknown arg:

    D:\download\性能工具\JMeter\apache-jmeter-2.11\apache-jmeter-2.11\bin>jmeter -n -t E:\性能测试\jmeter scr ...

  5. python16_day07【class】

    一.初识类 1.类的两种作用:属性引用和实例化 class Garen: #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄; camp='Demacia' #所有玩家的英雄(盖伦)的阵营都是Dem ...

  6. go——反射

    反射(reflect)让我们能在运行期探知对象地类型信息和内存结构,这从一定程度上弥补了静态语言在动态行为上地不足.和C数据结构一样,Go对象头部并没有类型指针,通过其自身是无法在运行期获知任何类型相 ...

  7. 并查集模板 && 带权并查集模板

    不带权: ]; void init(void) { ;i<=n;i++) f[i]=i; } int fd(int x) { return f[x]==x?x:fd[x]=fd(f[x]); } ...

  8. 自动化测试管理平台ATMS(V1.0.1_7.29)下载

    自动化测试管理平台ATMS(V1.0.1_7.29)下载http://automationqa.com/forum.php?mod=viewthread&tid=2582&fromui ...

  9. 属性检测 In,hasOwnPreperty()和propertyIsEnumerable()

    IN  左侧是属性名:右侧是对象名, 如果 属性是 自有属性 或者继承属性 则返回 TRUE var o={x:1,y:2} "x" in  o    返回 true: hasOw ...

  10. 【c++习题】【17/4/16】动态分配内存

    #include<iostream> #include<cstring> #define N 100 using namespace std; class String{ pu ...