原题如下:

思路:将nums放到一个map<int,int>中,其中,键是nums中元素,值对应其下标。然后遍历nums,取nums中一个值nums[i],接着用target减去它,最后再map中找差值map[num[i]]。如果发现差值,则返回i,map[num[i]]。

代码如下:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> vec;
map<int,int> im;
//将nums装进map容器中,键为vector元素,值为数组下标
for(int i = ;i<nums.size();i++)
{
im[nums[i]]=i;
} map<int,int>::iterator it;
//遍历vector作差,然后再map中寻找差值,如果命中,则记录下标
for(int i = ;i<nums.size();i++)
{
int sub = target - nums[i];
it = im.find(sub);
if(it != im.end() && it->second != i)
{
vec.push_back(i);
vec.push_back(it->second);
break;
}
}
return vec;
}

1_Two Sum --LeetCode的更多相关文章

  1. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

  2. 1_Two Sum

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  3. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  4. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. Nested List Weight Sum -- LeetCode 339

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  6. Combination Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...

  7. two Sum ---- LeetCode 001

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

  8. Minimum Size Subarray Sum -- leetcode

    题目描写叙述: Given an array of n positive integers and a positive integer s, find the minimal length of a ...

  9. Minimum Size Subarray Sum —— LeetCode

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

随机推荐

  1. 编译原理:基于状态转换图识别for语句

    int state =0;while(state<9){ switch state{ case 0: if(ch=='f'){ state=1;getchar(ch); } case 1: if ...

  2. 查看Eclipse运行工程时使用的Command Line

    一.查看使用的Command Line 1.Window -> Show View -> Other... -> Debug 2.运行工程,然后在Debug视窗中找到运行的工程的主线 ...

  3. Django REST framework反向生成url

    Django REST framework是一个基于Django的框架,REST framework又是怎么反向生成url的呢?? 在前面的例子中,知道在REST framework中有6种版本控制的 ...

  4. VBoxManage命令速记

    1.IDE控制器创建:VBoxManage storagectl testvm --name "IDE Controller" --add ide卸载VBoxManage stor ...

  5. Yii框架中使用mongodb扩展

    前提条件:安装了mongodb数据库 安装了mongo的php驱动 下载Yii的mongo扩展:这是YiiMongoDbSuite的1.3.6版本支持PHP Mongo驱动的版本为1.0.5及以下 下 ...

  6. SpringMVC源码情操陶冶-AbstractUrlHandlerMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,前文主要讲解了如何获取handler处理对象,本文将针对beanName注册为handler对象作下解析 Abst ...

  7. Hadoop学习笔记四

    一.fsimage,edits和datanode的block在本地文件系统中位置的配置 fsimage:hdfs-site.xml中的dfs.namenode.name.dir  值例如file:// ...

  8. 济南清北学堂游记 Day 2.

    在大佬云集的地方被直线碾压是什么样的体验? 大概就是210和1030的差别. 大概就是高质量机械键盘和空气的区别. 回来的路上,我一直在想,我到底是不是一个高三的? 大概也是能找到以前在家和学校训练时 ...

  9. BZOJ 3456: 城市规划 [多项式求逆元 DP]

    题意: 求出n个点的简单(无重边无自环)无向连通图数目.方案数mod 1004535809(479 * 2 ^ 21 + 1)即可. n<=130000 DP求方案 g(n) n个点所有图的方案 ...

  10. new function

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...