前言

 

【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. scrapy框架的持久化存储

    一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...

  2. Spring MVC起步

    1.1跟踪Spring MVC的请求 每当用户在Web浏览器中点击链接或提交表单的时候,请求就开始工作了.对请求的工作描述就像是快递投送员.与邮局投递员或FedEx投送员一样,请求会将信息从一个地方带 ...

  3. log4j:WARN No appenders could be found for logger 解决办法

    转自:https://blog.csdn.net/chw0629/article/details/80567936 使用log4j时不起作用,每次执行完出现以下提示: log4j:WARN No ap ...

  4. 我的Linux之路——虚拟机linux与主机之间的文件传送

    出自:https://jingyan.baidu.com/article/d169e186a00422436711d872.html FTP工具或者FTP命令(put.get) 常用的工具如:Xftp ...

  5. Python之路,Day9 , IO多路复用(番外篇)

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  6. U3D中物体的渲染顺序

    1,由SHADER中渲染队列及队列中的值决定 2,在同一队列中,若材质相同 2.1 对于UI,按其在场景层级中的先后顺序绘制 2.2 对于3D不透明物体,按其离相机的距离,由近到远绘制,这样可以减少像 ...

  7. 使用crontab设置定时任务

    配置文件 crontab主要的配置文件如下: /etc/crontab:系统cron表 /etc/cron.d/*:保存由软件包安装脚本创建的cron文件的目录 /var/spool/cron/*:保 ...

  8. 强大的vim配置文件,让编程更随意 (转载)

    花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2. ...

  9. C# ValueTypes

    [C# ValueTypes] 1.哪些类型是ValueType? The value types consist of two main categories: Structs Enumeratio ...

  10. 调试bug心得

    1. 忌主观主义.遇到问题,优先考虑客观排除法,按模块一一屏蔽测试排除.忌自己认为某个模块里的某个逻辑有问题,一直研究修改测试,被其他模块导致的偶然事件所干扰.通过排除法,找到震源,再解决.