题目:

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. 浅谈<持续集成、持续交付、持续部署>(一)

    谈谈持续集成,持续交付,持续部署之间的区别 经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢?   假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集 ...

  2. 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组

    [BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...

  3. cocos2dx使用cocostudio导出的scene

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  4. JVM垃圾回收时的可触及性

    可触及的 1.从根节点可以触及到这个对象可复活的 1.一旦所有引用被释放,就是可复活状态 2.因为在finalize()中可能复活该对象不可触及的 1.在finalize()后,可能会进入不可触及状态 ...

  5. Kattis - entertainmentbox 【贪心】

    思路 先将 N 个 电视节目 排序 根据 结束时间 ,结束的早的 排在前面 然后 弄 K个标记 记录 结束时间 然后 遍历一下 每次 如果能插入的话 插入到 结束时间最小的那个 队列里面去然后 每次插 ...

  6. Python OOP(3) staticmethod和classmethod统计实例

    staticmethod 统计实例 #!python2 #-*- coding:utf-8 -*- class c1: amount_instance=0 def __init__(self): c1 ...

  7. Linux系统中UI库curse.h不存在问题——贪吃蛇为例

    1. 问题 大家在用Linux写程序时,大家会使用Linux gcc编译器中的头文件curse.h.但往往一般的发行版中都没有默认安装这个头文件,需要大家自行安装.最近遇到这个问题,如下: Red  ...

  8. Unity 中 GetComponentsInChildren 的应用

    在实际项目中,我们经常要去查找一个节点下的某个子节点,但是子节点太多,或者每次我们都要去自己写GameObject.FindChald("xxx")实在是太过繁琐,那么这是后就可以 ...

  9. android OTA升级包制作【转】

    本文转载自:http://www.thinksaas.cn/topics/0/445/445670.html 0.签名 java -Xmx2048m -jar out/host/linux-x86/f ...

  10. hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

                                                                                Binary Tree Traversals T ...