原题如下:

思路:将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. Markdown内嵌Html语言

    概述 Markdown是内嵌Html语言的,这使得我们可以在Markdown文档里面实现很多有趣的东西.现在记录在此,供自己以后参考,相信对其他人也有用. 介绍 Markdown的语法只有一个目标:作 ...

  2. Asp.net Core 入门实战 2.请求流程

    Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone,喜欢的(Star),本系列持续更新,也可以通过我的网站访问 ...

  3. 关于define和const

    1.通过define定义的常量,在C语言里面一般叫宏定义.define的本质是简单的文本替换. 2.const定义一个变量,但是这个变量的值只能在定义的时候赋予,之后就不能被更改了. 如果变量声明中带 ...

  4. 【Java】泛型学习笔记

    参考书籍 <Java核心技术:卷1> 泛型, 先睹为快 先通过一个简单的例子说明下Java中泛型的用法: 泛型的基本形式类似于模板, 通过一个类型参数T, 你可以"私人定制&qu ...

  5. PHP支付第3方接口使用方法。

    去年写过一遍博客文章. 网站申请不到支付宝接口.微信接口,免接口收款实现方式. 网络在发展,支付宝也好,微信也好,技术在进步,这种方式已经不能使用了,明显的一个问题是,支付宝的刷新工具,会定时退出,必 ...

  6. Round trip 流程图

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!

  7. Apache 配置SSI速记

    1. 启用模块 httpd.conf LoadModule filter_module modules/mod_filter.so 2. <Directory 的Options配置中增加Incl ...

  8. SEO页面优化以及如何对单页面应用进行SEO优化

    一.简介 1.何为SEO? SEO(search engine optimization),翻译为搜索引擎优化,是利用搜索引擎的搜索规则来提高在相关搜索引擎的排名以及访问量的方式. 2.目的 为了获取 ...

  9. .Neter玩转Linux系列之三:Linux下的分区讲解

    基础篇 .Neter玩转Linux系列之一:初识Linux .Neter玩转Linux系列之二:Linux下的文件目录及文件目录的权限 .Neter玩转Linux系列之三:Linux下的分区讲解 .N ...

  10. 在eclipse的配置文件里指定jdk路径

    在eclipse的配置文件里指定jdk路径,只需在eclipse的配置文件里增加-vm参数即可. 打开eclipse目录下的eclipse.ini配置文件,增加-vm配置,需要注意的是该参数要加在-v ...