题意:给定一个目标值target,找到两个数的和为target,返回这两个数的下标

用map记录每个数的下标,并用于查找

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

  

Leetcode 1 Two Sum STL的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. easyul获取各种属性ID 和赋值

    //span赋值 $('#state1').text("审核通过"); //textarea赋值 $("#memo").val('');  //隐藏域 $(&q ...

  2. 简单的jquery插件写法之一

    http://jsfiddle.net/kyu0hdmx/embedded/#HTML

  3. 集合框架之 List

    集合框架就是Java中解决数组长度固定的问题,相当于动态数组,随时用,随时建立,内存释放  这个问题还不知道? 整个集合框架 概括为   List  Set Map 这三个接口以及他们的实现类之间的应 ...

  4. 单片机TM4C123学习(八):SPI接口D/A

    1.头文件和变量定义(不是很清楚) #include "driverlib/ssi.h" #include "driverlib/i2c.h" #include ...

  5. iOS项目中常用的第三方开源库

    1.项目使用的第三方开源库 项目使用了CocoaPods(类似java中的maven)管理常用的第三方库,一些特殊的单独引用,下面介绍下比较好用的几个. (1)AFNetworking 目前比较推荐的 ...

  6. Z Fighting Problem

    Here is a video about unity depth shader workarounds: http://www.burgzergarcade.com/tutorials/game-e ...

  7. 在Mac OS X中配置Apache

    启动Apache 有两种方法: 打开“系统设置偏好(System Preferences)” -> “共享(Sharing)” -> “Web共享(Web Sharing)” 打开“终端( ...

  8. Hibernate个人学习笔记(1)

    连接池c3p0所需jar包:Hiberbate开发包-lib-optional-c3p0下全部Jar包 Hiberbate连接池参数配置:Hiberbate开发包-project-etc-hibern ...

  9. Navicat for Oracle 连接oracle 配置

    oci.dll  替换为对应oracle版本的oci.dll

  10. windows 硬盘格式不一样的文件移动 导致拒绝访问 权限丢失 0字节解决办法

    解决此问题,必须关闭“简单文件共享”,然后获取文件夹的所有权: 1. 关闭“简单文件共享”: a. 单击“开始”,然后单击“我的电脑”. b. 在“工具”菜单上,单击“文件夹选项”,然后单击“查看”选 ...