前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

 

2. 题意

给定一组数字,找到两个元素,使得两者的和满足一个给定的目标值Target。

返回值要求:输出的两个数,index1 必须小于index2.

3. 思路

1. 暴力BF,直接超时

2. hash,用映射 STL: map + vector

4. 解法

class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int ,int> match;
vector<int> ans;
ans.clear();
int i;
for(i=0;i<numbers.size();i++)
match[numbers[i]] = i+1; // 记录位置
for(i=0;i<numbers.size();i++)
{ if(match.find(target - numbers[i]) != match.end() && i+1 != match[target - numbers[i]]) // map映射
{
ans.push_back(i+1);
ans.push_back(match[target-numbers[i]]);
break;
}
}
return ans;
}
};

 

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3751836.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解]: Two Sum的更多相关文章

  1. [LeetCode 题解] Combination Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a se ...

  2. LeetCode题解——Two Sum

    题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...

  3. LeetCode题解之Sum Root to Leaf Numbers

    1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...

  4. 回溯法 leetcode题解 Combination Sum 递归法

    题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...

  5. LeetCode题解之 Sum of Left Leaves

    1.题目描述 2.问题分析 对于每个节点,如果其左子节点是叶子,则加上它的值,如果不是,递归,再对右子节点递归即可. 3.代码 int sumOfLeftLeaves(TreeNode* root) ...

  6. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

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

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

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

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

  9. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

随机推荐

  1. 13_java之final|static|包|匿名对象|代码块|内部类

    01final关键字概念 * A: 概述 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承 ...

  2. 关于网格比较工具metro使用的几点注意事项

    Metro作为一个非常好用的简化网格比较工具,在科研界几乎算是标准了.不过很多比较牛的作者会使用自己设计的一些比较算法,但是如果metro够用了也就不必那么麻烦了,毕竟Metro使用的方法还算是很成熟 ...

  3. windows兼容方式安装python[转]

    Python 是一门很不错的语言,语言简单易学,又不失脚本语言的灵活性,还有海量的第三方库,覆盖的很全面.但也有不少“硬伤”,比如 Python 2.x 和 Python 3.x 版本之间的不兼容等等 ...

  4. Newtonsoft.Json(Json.Net)学习

    转自原文 Newtonsoft.Json(Json.Net)学习笔记 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库.软件下载地址: http://www.newto ...

  5. centos 和KVM安装

  6. Eclipse 常用插件地址大全

    安装方式: 使用Eclipse 的自动升级功能,菜单栏选[ Help ]→[ install new Software]  点击[Add]按钮,在“ Name ”中填入“ name (填写一个自己喜欢 ...

  7. Spark会产生shuffle的算子

    去重 def distinct() def distinct(numPartitions: Int) 聚合 def reduceByKey(func: (V, V) => V, numParti ...

  8. socket,TCP/IP的理解(转)

    TCP/IP 要想理解socket首先得熟悉一下TCP/IP协议族, TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间 ...

  9. Ajax与Controller的参数交互

    理论 jQuery.ajax( options )中重要参数设置 jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据.通过jquery.ajax与SpringMVC的C ...

  10. 设计模式--适配器模式(Adapter)详解

    适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式. 01.类的适配器模式 核心 ...