题意:给定一个目标值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. Antenna Placement poj 3020(匹配)

    http://poj.org/problem?id=3020 题意:给定一个n*m的矩阵,'*'代表城市,现在想要用1*2的矩阵将所有的城市覆盖,问最少需要多少个矩阵? 分析:先为每个城市进行标号,再 ...

  2. ArcGIS删除部分数据后全图范围不正确

      我有一个全国地图的图层,现在删除图层中其他省份,只保留山东省的图形,但是点击全图后,全图范围仍然是全国地图时候的全图范围,使用的版本是ArcGIS9.3,数据存放在9.3的个人数据库中(Perso ...

  3. 查找SQL数据表或视图中的字段属性信息

    一.只支持表,非常牛逼的 SELECT a.name,(case when (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name F ...

  4. cookie 的“Value”=“xxxxx,xxxxx”部分无效

    cookie 的“Value”=“xxxxx,xxxxx”部分无效 在一些网站中有时候会遇到Cookie的值为逗号 但是在.Net中Cookie的值是不能直接使用逗号的 如果使用形如 C#代码 1.C ...

  5. 子元素的div不继承父元素的透明度

    问题一: 如何让logo部变成这样: 步骤一:先收一个大的div,上面logo+nav再一个div,下面是三个div横排着: 步骤二:最主要的是如何让上面的部分变暗,而logo的颜色不变暗? 答案:在 ...

  6. Oracle 建表,递增序列,触发器,分析函数row_number() ,partition by 子句。

    create table SC ( Id INTEGER, Name nvarchar2(20) , KC_Name nvarchar2(20), KC_score INTEGER , constra ...

  7. hdu 1231

    最大连续子序列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  8. ZooKeeper在centos6.4的集群搭建

    首先给一个小tips,在搭建zookeeper之前,需要配置好java环境,请参看我的另一篇文章<Jdk1.8在CentOS7中的安装与配置>,还需要免密码登录,请参看我的另一篇文章< ...

  9. 9月15日,YTFCloud,创业圈的技术新宠

    成都创业圈近期什么最热? 资本复苏?这本来就是高低起伏规律而已,再说动辄千万的大手笔,对于创业新人.新团队来说太遥远,早期根本无法变现,而资金紧张.项目开发费用超标.没有技术大牛保障等尖锐现实问题能否 ...

  10. 【数位dp】bzoj2089 不要62

    http://www.cnblogs.com/xiaohongmao/p/3473599.html #include<cstdio> using namespace std; int n, ...