题目:

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 sameelement twice.

Example:

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

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

题解:

Solution 1 (my submmision)

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> map;
for(int i = ; i < nums.size(); ++i){
int val = target - nums[i];
if(map.find(val) != map.end()){
res.push_back(map[val]);
map[nums[i]] = i;
res.push_back(map[nums[i]]);
break;
}
map[nums[i]] = i;
}
return res;
}
};

Solution 2

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> map;
for(int i = ; i < nums.size(); ++i){
if(map.find(nums[i]) == map.end()){
map[target - nums[i]] = i;
} else {
res.push_back(map[nums[i]]);
res.push_back(i);
break;
}
}
return res;
}
};

相当于建立数组中每个元素的相对应的加数的集合,这个加数对应了原数组中元素的坐标。

【LeetCode】001. Two Sum的更多相关文章

  1. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  4. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  5. 【LeetCode】#1 Two Sum

    [Question] Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  7. 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  8. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

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

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

随机推荐

  1. (转)深入理解Java内存模型之系列篇

    原文地址: http://blog.csdn.net/ccit0519/article/details/11241403 深入理解Java内存模型(一)——基础 并发编程模型的分类 在并发编程中,我们 ...

  2. [原创]webpack动态设置css路径

    在程序入口的最上方添加 __webpack_public_path__ = path; //your path //your app start here

  3. mac上好用的软件

    1 newfile menu for Mac 右键创建文件.

  4. iOS Load方法 和 initialize方法的比较

    一.load方法特点: 1. 当类被引用进程序的时候会执行这个函数 2.一个类的load方法不用写明[super load],父类就会收到调用,并且在子类之前. 3.Category的load也会收到 ...

  5. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之开发游戏界面(二)

    连连看的游戏界面十分简单,大致可以分为两个区域: 游戏主界面区 控制按钮和数据显示区 1.开发界面布局 本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件, ...

  6. python cookbook第三版学习笔记十六:抽象基类

    假设一个工程中有多个类,每个类都通过__init__来初始化参数.但是可能有很多高度重复且样式相同的__init__.为了减少代码.我们可以将初始化数据结构的步骤归纳到一个单独的__init__函数中 ...

  7. dockerfile nginx配置

    Dockerfile 代码 From hub.c.163.com/public/nginx:1.2.1 RUN rm -v /etc/nginx/nginx.conf ADD nginx.conf / ...

  8. ARDUINO使用GPRS发送GPS数据到OneNet测试

    功能: 测试把固定的GPS数据发送到OneNet平台 调试途中碰到的问题 ARDUINO不支持sprintf的double打印,只能转换为char字符串然后再%s打印 #include <Tim ...

  9. VS2015 下载 破解

    Visual Studio Professional 2015简体中文版(专业版): http://download.microsoft.com/download/B/8/9/B898E46E-CBA ...

  10. Shell中的while循环

    while循环的格式   while expression do command command ``` done 1.计数器控制的while循环    主要用于已经准确知道要输入的数据和字符串的数目 ...