原题如下:

思路:将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. jQuery图片延迟加载插件jquery.lazyload.js

    在实际的项目开发中,我们通常会遇见这样的场景:一个页面有很多图片,而首屏出现的图片大概就一两张,那么我们还要一次性把所有图片都加载出来吗?显然这是愚蠢的,不仅影响页面渲染速度,还浪费带宽.这也就是们通 ...

  2. spring boot热部署

    1.pom配置 参考:http://412887952-qq-com.iteye.com/blog/2300313 2.intellij配置 参考:http://blog.csdn.net/wjc47 ...

  3. NodeJs实现他人项目实例

    1.简单实例,参考 https://github.com/alsotang/node-lessons/tree/master/lesson2 2.express一个新项目 ,但出现警告 发现少了nod ...

  4. FastDFS分布式存储实战

    <FastDFS分布式存储实战> 技术选型 FastDFS相关组件及原理 FastDFS介绍 FastDFS架构 FastDFS工作流程 上传 同步机制 下载 文件合并原理 实验环境说明 ...

  5. Hadoop-2.6.5安装

    简介 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoop实现了一个分布式文 ...

  6. WPF中,多key值绑定问题,一个key绑定一个界面上的对象

    问题说明: 当用到dictionary<key,value>来储存数据的时候,有时候需要在界面上绑定一个key来显示value,这时候有两种思路: 一种是写一个自定义的扩展类,类似Bind ...

  7. c# 简单实现 插件模型 反射方式

    利用反射方式实现插件模型,wpf控件作为插件,然后用另外的窗体加载. 首先定义插件接口: public interface IUserControlLevel1 { string PluginName ...

  8. mybatis No enum const class org.apache.ibatis.type.JdbcType.Integer

    mybatis报错:没有Integer这个类型的jdbcType值 原因:mybatis配置重的jdbaType类型要是大写的 如图所示:

  9. selenium打开chrome浏览器代码

    import os from selenium import webdriver chromedriver = "C:\Program Files (x86)\Google\Chrome\A ...

  10. Java内存回收机制基础[转]

    原文链接:http://blog.jobbole.com/37273/ 在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了J ...